Adam Burley
Adam Burley

Reputation: 6069

Why does the npm userconfig not get picked up?

By default, NPM stores the user config in ~/.npmrc. I had a load of stuff in my user config which I didn't want to interfere with for my main project, but I was switching to a second project which required different configuration, so I just did:

npm config set userconfig C:\path\to\another\directory\.npmrc.

To my surprise, NPM added a new entry userconfig into my existing user config at ~/.npmrc. This doesn't make sense to me - seems NPM needs to know where the user config is in order to find out where it is!

Now NPM ignores any properties I put into my new NPMRC file and only takes properties from ~/.npmrc. Even more strangely, npm config list contains a userconfig header (semicolon-prefixed line) specifying my new location, but the contents come from ~/.npmrc.

For example, if I set my email as "[email protected]" in the ~/.npmrc and as "[email protected]" in my new NPMRC, npm config list reports something like this:

; cli configs
user-agent = "npm/3.10.8 node/v6.9.1 win32 x64"

; userconfig C:\path\to\another\directory\.npmrc
(... other properties ...)
email = "[email protected]"
(... other properties ...)
userconfig = "C:\path\to\another\directory\.npmrc"

; builtin config undefined

; node bin location = C:\Program Files\nodejs\node.exe
; cwd = C:\
; HOME = C:\Users\MyUsername
; "npm config ls -l" to show all defaults.

The value has come from ~/.npmrc despite appearances (I double-checked the value in the new NPMRC is definitely "[email protected]").

I thought this might be a bug in NPM so I upgraded my Node/NPM to the latest version, but it still behaves the same way. Am I using userconfig wrong and how should it work?

Upvotes: 13

Views: 25854

Answers (1)

Shammoo
Shammoo

Reputation: 1103

From https://docs.npmjs.com/misc/config

npmrc Files

The four relevant files are:

  • per-project configuration file (/path/to/my/project/.npmrc)
  • per-user configuration file (defaults to $HOME/.npmrc; configurable via CLI option --userconfig or environment variable $NPM_CONFIG_USERCONFIG)
  • global configuration file (defaults to $PREFIX/etc/npmrc; configurable via CLI option --globalconfig or environment variable $NPM_CONFIG_GLOBALCONFIG)
  • npm's built-in configuration file (/path/to/npm/npmrc)

Fix

Call a command like npm --userconfig /another/path/to/npmrc install and it will use that instead of the ~/.npmrc file

Upvotes: 12

Related Questions