Skip to content

Commit 891e7fa

Browse files
committed
feat: read package.json config values
Also will read config environment variables in the same format that npm@11 will set them when read from the package.json Fixes #3156
1 parent 71a910e commit 891e7fa

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,25 +235,44 @@ Some additional resources for Node.js native addons and writing `gyp` configurat
235235

236236
## Configuration
237237

238+
### package.json
239+
240+
Use the `config` object in your package.json with each key in the form `node_gyp_OPTION_NAME`. Any of the command
241+
options listed above can be set (dashes in option names should be replaced by underscores).
242+
243+
For example, to set `devdir` equal to `/tmp/.gyp`, your package.json would contain this:
244+
245+
```json
246+
{
247+
"config": {
248+
"node_gyp_devdir": "/tmp/.gyp"
249+
}
250+
}
251+
```
252+
238253
### Environment variables
239254

240-
Use the form `npm_config_OPTION_NAME` for any of the command options listed
255+
Use the form `npm_package_config_node_gyp_OPTION_NAME` for any of the command options listed
241256
above (dashes in option names should be replaced by underscores).
242257

243258
For example, to set `devdir` equal to `/tmp/.gyp`, you would:
244259

245260
Run this on Unix:
246261

247262
```bash
248-
export npm_config_devdir=/tmp/.gyp
263+
export npm_package_config_node_gyp_devdir=/tmp/.gyp
249264
```
250265

251266
Or this on Windows:
252267

253268
```console
254-
set npm_config_devdir=c:\temp\.gyp
269+
set npm_package_config_node_gyp_devdir=c:\temp\.gyp
255270
```
256271

272+
Note that in versions of npm before v11 it was possible to use the prefix `npm_config_` for
273+
environement variables. This was deprecated in npm@11 and will be removed in npm@12 so it
274+
is recommened to convert your environment variables to the above format.
275+
257276
### `npm` configuration for npm versions before v9
258277

259278
Use the form `OPTION_NAME` for any of the command options listed above.

lib/node-gyp.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,29 @@ class Gyp extends EventEmitter {
145145
}
146146
})
147147

148+
// Read values that npm sets based on the `config` object in package.json.
149+
// These take precendence over the npm_config_ prefixed values which
150+
// are deprecated in npm@11.
151+
// These can also be set directly in the environment, if setting in
152+
// package.json is not desired.
153+
const npmPackageConfigPrefix = 'npm_package_config_node_gyp_'
154+
Object.keys(process.env).forEach((name) => {
155+
if (name.indexOf(npmPackageConfigPrefix) !== 0) {
156+
return
157+
}
158+
const val = process.env[name]
159+
name = name.substring(npmPackageConfigPrefix.length)
160+
// gyp@741b7f1 enters an infinite loop when it encounters
161+
// zero-length options so ensure those don't get through.
162+
if (name) {
163+
// convert names like force_process_config to force-process-config
164+
if (name.includes('_')) {
165+
name = name.replace(/_/g, '-')
166+
}
167+
this.opts[name] = val
168+
}
169+
})
170+
148171
if (this.opts.loglevel) {
149172
log.logger.level = this.opts.loglevel
150173
}

0 commit comments

Comments
 (0)