TExcelTAD

.npmrc & Configuration

Every npm option you can pass on the command line (--registry, --save-exact, --legacy-peer-deps) can also live in an .npmrc file, as key=value lines. pnpm reads the same files, and Yarn classic does too; Yarn Berry has its own .yarnrc.yml. Knowing which file wins saves hours of "but it works on my machine".

The layers

From highest to lowest priority:

Source Example Typical use
command line --registry=… one-off
environment NPM_CONFIG_REGISTRY=… (also lowercase npm_config_…) CI
project <project>/.npmrc, committed team-wide settings: registry, save-exact, engine-strict
user ~/.npmrc your auth tokens
global <prefix>/etc/npmrc machine-wide defaults
built-in inside npm defaults

npm config ls shows the effective settings and which file each came from:

bash · try it yourself
mkdir -p proj && cd proj
npm init -y > /dev/null
printf 'save-exact=true\nengine-strict=true\n' > .npmrc
npm config ls | grep -v '^;.*node bin\|^; node version\|^; npm \|^; cwd\|^; HOME\|^; Run'
; "global" config from /opt/node/etc/npmrc

audit = false
fund = false
progress = false
registry = "http://localhost:4873/"
; update-notifier = false ; overridden by env

; "user" config from /home/learner/.npmrc

//localhost:4873/:_authToken = (protected)

; "project" config from /home/learner/proj/.npmrc

engine-strict = true
save-exact = true
save-prefix = ""

; "env" config from environment

update-notifier = false

In this container the registry and the quiet output settings come from the global file /opt/node/etc/npmrc, the login token from ~/.npmrc (printed as (protected)), and the project file adds two settings of its own (save-prefix = "" is derived from save-exact). update-notifier is set twice; the line from the global file is commented out with overridden by env, because the container also sets NPM_CONFIG_UPDATE_NOTIFIER.

Precedence in action

bash · try it yourself
mkdir -p proj && cd proj
npm init -y > /dev/null
npm config get save-exact
echo 'save-exact=true' > .npmrc
npm config get save-exact
NPM_CONFIG_SAVE_EXACT=false npm config get save-exact
npm config get save-exact --save-exact=false
npm install ms@2.1.3 > /dev/null
npm pkg get dependencies
false
true
false
false
{
  "ms": "2.1.3"
}

The project file switched save-exact on, so npm install ms wrote "2.1.3" instead of "^2.1.3"; an environment variable or a flag would override it again.

Registries, scopes and tokens

Most companies run a private registry (Verdaccio, Artifactory, Nexus, GitHub Packages) for their own @company/* packages, often proxying the public registry too. .npmrc can route by scope:

ini
# project .npmrc (committed)
registry=https://registry.npmjs.org/
@acme:registry=https://npm.acme.internal/
save-exact=true
engine-strict=true

# auth for that host: a token from the environment, never a literal secret
//npm.acme.internal/:_authToken=${NPM_TOKEN}
  • Auth settings are keyed by host and path (//npm.acme.internal/:_authToken), so a token is only ever sent to the registry it belongs to.
  • ${NPM_TOKEN} is expanded from the environment when npm reads the file, so the committed file contains no secret.
  • npm login --registry=… writes such a line into ~/.npmrc for you.

A scope mapping, live:

bash · try it yourself
mkdir -p proj && cd proj
npm init -y > /dev/null
echo '@nowhere:registry=http://localhost:9/' > .npmrc
npm config get @nowhere:registry
npm install @nowhere/thing --fetch-retries=0 2>&1 | grep -E 'code|request to' | cut -c1-80
npm install ms@2.1.3 2>&1 | grep added
http://localhost:9/
npm error code ECONNREFUSED
npm error FetchError: request to http://localhost:9/@nowhere%2fthing failed, rea
npm error   code: 'ECONNREFUSED',
added 1 package in 444ms

The @nowhere scope was sent to its own (dead) registry while unscoped ms still came from the default one. This routing is also the main defence against dependency confusion: if @acme/* always resolves from your registry, nobody can trick your build into fetching a public package with the same name.

Settings worth knowing

Setting Effect
registry, @scope:registry where packages come from
save-exact=true write 1.2.3 instead of ^1.2.3
engine-strict=true fail when engines doesn't match
ignore-scripts=true never run install scripts
strict-allow-scripts=true fail on install scripts not in allowScripts
fund=false, audit=false, update-notifier=false, progress=false quieter installs (set in this container)
prefer-offline=true use the cache without revalidating when possible
before=<date> resolve as if it were that date: only versions published before it
min-release-age skip versions younger than a given age, a guard against freshly hijacked releases (Yarn: npmMinimalAgeGate)
script-shell the shell npm run uses
prefix where -g installs go

pnpm reads .npmrc for registry and auth too, plus its own settings in pnpm-workspace.yaml. Yarn Berry ignores .npmrc entirely: its equivalents (npmRegistryServer, npmScopes, npmAuthToken) live in .yarnrc.yml:

bash · try it yourself
cat ~/.yarnrc.yml | sed 's/npmAuthToken: .*/npmAuthToken: "(token)"/'
npmRegistryServer: "http://localhost:4873"
npmAuthToken: "(token)"
unsafeHttpWhitelist:
  - localhost
enableTelemetry: false
enableProgressBars: false
enableGlobalCache: true
npmMinimalAgeGate: 0

Test yourself

A project .npmrc says save-exact=true and CI sets NPM_CONFIG_SAVE_EXACT=false. Which wins?

  1. The project file
  2. The environment variable
  3. Whichever was set last
  4. Neither; npm errors

How should a committed .npmrc reference a registry token?

  1. Paste the token
  2. Use ${NPM_TOKEN} so it's expanded from the environment
  3. Base64-encode it
  4. Put it in package.json instead

What does @acme:registry=https://npm.acme.internal/ do?

  1. Publishes every package to acme
  2. Resolves only @acme/* packages from that registry
  3. Blocks the @acme scope
  4. Sets the default registry