Skip to content

Commit 9fac412

Browse files
authored
fix: improve unknown config warning with .npmrc section hint (#8995)
1 parent aa8ffbf commit 9fac412

6 files changed

Lines changed: 50 additions & 20 deletions

File tree

docs/lib/content/configuring-npm/npmrc.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,33 @@ _authToken=MYTOKEN
157157
//somewhere-else.com/another/:_authToken=MYTOKEN2
158158
```
159159

160+
### Custom / third-party config keys
161+
162+
npm only recognizes its own [configuration options](/using-npm/config).
163+
If your `.npmrc` contains keys that are not part of npm's config definitions
164+
(for example, `electron_mirror` or `sass_binary_site`), npm will emit a
165+
warning:
166+
167+
```
168+
warn Unknown user config "electron_mirror".
169+
This will stop working in the next major version of npm.
170+
```
171+
172+
These keys were historically tolerated but are not officially supported.
173+
A future major version of npm will treat unknown top-level keys as errors.
174+
175+
Some tools (such as `@electron/get` or `node-sass`) read their own
176+
configuration from environment variables or from `.npmrc` by convention.
177+
You can set these values as environment variables instead:
178+
179+
```bash
180+
export ELECTRON_MIRROR="https://mirrorexample.npmjs.org/mirrors/electron/"
181+
export ELECTRON_CUSTOM_DIR="{{ version }}"
182+
```
183+
184+
Environment variables are the most portable approach and work regardless
185+
of `.npmrc` format.
186+
160187
### See also
161188

162189
* [npm folders](/configuring-npm/folders)

tap-snapshots/test/lib/commands/config.js.test.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,6 @@ registry = "https://some.registry"
451451

452452
exports[`test/lib/commands/config.js TAP config list with publishConfig local > warns about unknown config 1`] = `
453453
Array [
454-
"Unknown publishConfig config /"other/". This will stop working in the next major version of npm.",
454+
"Unknown publishConfig config /"other/". This will stop working in the next major version of npm. See \`npm help npmrc\` for supported config options.",
455455
]
456456
`

test/lib/commands/publish.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ t.test('respects publishConfig.registry, runs appropriate scripts', async t => {
5555
t.equal(fs.existsSync(path.join(prefix, 'scripts-prepublish')), false, 'did not run prepublish')
5656
t.equal(fs.existsSync(path.join(prefix, 'scripts-publish')), true, 'ran publish')
5757
t.equal(fs.existsSync(path.join(prefix, 'scripts-postpublish')), true, 'ran postpublish')
58-
t.same(logs.warn, ['Unknown publishConfig config "other". This will stop working in the next major version of npm.'])
58+
t.same(logs.warn, ['Unknown publishConfig config "other". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.'])
5959
})
6060

6161
t.test('re-loads publishConfig.registry if added during script process', async t => {

test/lib/commands/unpublish.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ t.test('publishConfig no spec', async t => {
409409
t.equal(joinedOutput(), '- test-package')
410410
t.same(logs.warn, [
411411
'using --force Recommended protections disabled.',
412-
'Unknown publishConfig config "other". This will stop working in the next major version of npm.',
412+
'Unknown publishConfig config "other". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.',
413413
])
414414
})
415415

workspaces/config/lib/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,13 +608,16 @@ class Config {
608608
if (internalEnv.includes(key)) {
609609
return
610610
}
611+
const hint = where !== 'cli'
612+
? ' See `npm help npmrc` for supported config options.'
613+
: ''
611614
if (!key.includes(':')) {
612-
this.queueWarning(key, `Unknown ${where} config "${where === 'cli' ? '--' : ''}${key}". This will stop working in the next major version of npm.`)
615+
this.queueWarning(key, `Unknown ${where} config "${where === 'cli' ? '--' : ''}${key}". This will stop working in the next major version of npm.${hint}`)
613616
return
614617
}
615618
const baseKey = key.split(':').pop()
616619
if (!this.definitions[baseKey] && !this.nerfDarts.includes(baseKey)) {
617-
this.queueWarning(baseKey, `Unknown ${where} config "${baseKey}" (${key}). This will stop working in the next major version of npm.`)
620+
this.queueWarning(baseKey, `Unknown ${where} config "${baseKey}" (${key}). This will stop working in the next major version of npm.${hint}`)
618621
}
619622
}
620623
}

workspaces/config/test/index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ loglevel = yolo
381381
// warn logs are emitted as a side effect of validate
382382
config.validate()
383383
t.strictSame(logs.filter(l => l[0] === 'warn'), [
384-
['warn', 'Unknown builtin config "builtin-config". This will stop working in the next major version of npm.'],
385-
['warn', 'Unknown builtin config "foo". This will stop working in the next major version of npm.'],
384+
['warn', 'Unknown builtin config "builtin-config". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.'],
385+
['warn', 'Unknown builtin config "foo". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.'],
386386
['warn', 'invalid config', 'registry="hello"', 'set in command line options'],
387387
['warn', 'invalid config', 'Must be', 'full url with "http://"'],
388388
['warn', 'invalid config', 'proxy="hello"', 'set in command line options'],
@@ -399,13 +399,13 @@ loglevel = yolo
399399
['warn', 'invalid config', 'prefix=true', 'set in command line options'],
400400
['warn', 'invalid config', 'Must be', 'valid filesystem path'],
401401
['warn', 'config', 'also', 'Please use --include=dev instead.'],
402-
['warn', 'Unknown env config "foo". This will stop working in the next major version of npm.'],
403-
['warn', 'Unknown project config "project-config". This will stop working in the next major version of npm.'],
404-
['warn', 'Unknown project config "foo". This will stop working in the next major version of npm.'],
405-
['warn', 'Unknown user config "user-config-from-builtin". This will stop working in the next major version of npm.'],
406-
['warn', 'Unknown user config "foo". This will stop working in the next major version of npm.'],
407-
['warn', 'Unknown global config "global-config". This will stop working in the next major version of npm.'],
408-
['warn', 'Unknown global config "foo". This will stop working in the next major version of npm.'],
402+
['warn', 'Unknown env config "foo". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.'],
403+
['warn', 'Unknown project config "project-config". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.'],
404+
['warn', 'Unknown project config "foo". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.'],
405+
['warn', 'Unknown user config "user-config-from-builtin". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.'],
406+
['warn', 'Unknown user config "foo". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.'],
407+
['warn', 'Unknown global config "global-config". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.'],
408+
['warn', 'Unknown global config "foo". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.'],
409409
['warn', 'invalid config', 'loglevel="yolo"', `set in ${resolve(path, 'project/.npmrc')}`],
410410
['warn', 'invalid config', 'Must be one of:',
411411
['silent', 'error', 'warn', 'notice', 'http', 'info', 'verbose', 'silly'].join(', '),
@@ -600,12 +600,12 @@ loglevel = yolo
600600
['warn', 'invalid config', 'prefix=true', 'set in command line options'],
601601
['warn', 'invalid config', 'Must be', 'valid filesystem path'],
602602
['warn', 'config', 'also', 'Please use --include=dev instead.'],
603-
['warn', 'Unknown env config "foo". This will stop working in the next major version of npm.'],
604-
['warn', 'Unknown user config "default-user-config-in-home". This will stop working in the next major version of npm.'],
605-
['warn', 'Unknown user config "foo". This will stop working in the next major version of npm.'],
606-
['warn', 'Unknown global config "global-config". This will stop working in the next major version of npm.'],
607-
['warn', 'Unknown global config "foo". This will stop working in the next major version of npm.'],
608-
['warn', 'Unknown global config "asdf". This will stop working in the next major version of npm.'],
603+
['warn', 'Unknown env config "foo". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.'],
604+
['warn', 'Unknown user config "default-user-config-in-home". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.'],
605+
['warn', 'Unknown user config "foo". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.'],
606+
['warn', 'Unknown global config "global-config". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.'],
607+
['warn', 'Unknown global config "foo". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.'],
608+
['warn', 'Unknown global config "asdf". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.'],
609609
])
610610
logs.length = 0
611611
})

0 commit comments

Comments
 (0)