npm Community Forum (Archive)

The npm community forum has been discontinued.

To discuss usage of npm, visit the GitHub Support Community.

Instruct npm to "reload" configuration (npmrc files)

When running npm commands from an npm script, how does one instruct npm to re-load configuration from npmrc files?

For example, say I have a "scripts": {"bootstrap": "node bootstrap.js"}, and bootstrap.js e.g. modifies the registry= line in the project/package directory and then runs npm install (child_process.execSync('npm install', ...). The npm install will not look at the new/updated per-project npmrc file, and the now-configured registry will not be respected.

I tried clearing the environment for the npm install command (e.g. retaining only PATH), but npm install still refuses to load the configuration. I tried detaching the npm install from the parent (child_process.spawnSync('npm, ['install'], {detached: true, ...})), but the problem persists.

How does npm detect this situation and decide to skip loading the configuration? How can I get it to re-load the configuration in this case? I can create an example package to demonstrate if the above isn’t clear; please let me know.

I don’t know for sure, but I would guess that npm checks if npm_config_* vars are present in the environment before loading from disk. It feels a bit like a hack to be monkeying with npm’s configuration from within a script, but I could see a few possible approaches:

  1. mutate the npm_config_* env vars directly, instead of mutating the npmrc file. (Which, IMO, should be considered static anyway and as a user, I would feel that no script should be touching my npmrc. It is a user-specific config file, after all.)

  2. Secondly, all of npm’s configuration options can be specified via CLI. So when you spawn npm from the bootstrap script, just pass the config option there as an argument. ie: --registry=

  3. Npm respects the .npmrc file within the project so add the registry configuration to that project-specific .npmrc file.

  4. Lastly, if these configuration options are meant to be for an entire organization, and if you control the machines on which development is done, you can provide machine-level npmrc file at $PREFIX/etc/npmrc

Option 3 is the preferred approach for setting package-specific npm configurations (like registry).