How to point npm at a private registry with .npmrc

Serving developers since 2016 Founder-run, no venture capital MFA, SSO and audit log on every plan

Every feature on every plan, with unlimited users. 14 days free. No credit card required.

Start free trial

Starting at $99/month billed annually.

Two lines in .npmrc route your own scope to a private registry and leave every other package on npmjs. One line sends everything through a single registry.

Which .npmrc npm reads

npm reads four files. npm’s docs list them in this order, and an earlier file overrides the same setting in a later one:

File Where it lives Use it for
Project .npmrc beside your package.json Registry lines for this project. Commit it.
User ~/.npmrc Your own tokens on your own machine.
Global $PREFIX/etc/npmrc Settings for everyone on a shared machine.
Built-in inside npm itself Leave it alone.

npm reads the project file only at the root of the project you run it in, and skips it completely for npm install -g.

Check what npm actually resolved:

npm config get registry
npm config get @yourscope:registry
npm config list

Send one scope to one registry

Most teams want this setup: packages named @yourscope/... come from your registry, and everything else still comes from npmjs.

@yourscope:registry=https://registry.example.com/npm/
//registry.example.com/npm/:_authToken=${NPM_TOKEN}
  • The first line maps the scope to your registry. A scope points to exactly one registry.
  • The second line is the credential. npm only sends a token to the registry its line names, so it starts with //, then the same host and path as the first line, with no https:. Keep the path and trailing slash identical on both lines.
  • ${NPM_TOKEN} pulls its value from that environment variable, so you can commit the file without a secret in it. If the variable isn’t set, npm leaves the text as it is and sends that, and the registry returns 401.

You can write the scope line from the command line instead:

npm config set @yourscope:registry=https://registry.example.com/npm/ --location=project

Send every package through one registry

One line changes npm’s default registry for every package, scoped or not:

registry=https://registry.example.com/npm/
//registry.example.com/npm/:_authToken=${NPM_TOKEN}

Use this only when that registry can answer for public packages too. If it holds only your own packages, every public install fails with 404 because npm no longer asks npmjs at all. To make a single registry= line work, point it at a registry that proxies npmjs, or a group that combines your packages with a proxy.

Check that it works

npm whoami --registry=https://registry.example.com/npm/
npm view @yourscope/your-package version

npm whoami asks the registry who owns the token, so a good response confirms your token line matches. Next, npm view confirms the scope line sends the package to the right place.

npm says Usual cause
E401 The registry rejected the token. It is mistyped, expired, or revoked, or NPM_TOKEN isn’t set in this shell or job.
E403 The token is valid, but isn’t allowed to read that package or repository.
E404 on a scoped package npm asked npmjs instead of your registry. The @yourscope:registry= line is missing, misspelled, or names another scope.
E404 on a public package registry= points to a registry that holds only your own packages.

Yarn and pnpm

  • pnpm reads auth, registry, TLS, and proxy settings from .npmrc, so the lines above work as written.
  • Yarn 2 and later read .yarnrc.yml instead. Here is the same scope config, and Yarn stops with an error if NPM_TOKEN isn’t set:
npmScopes:
yourscope:
npmRegistryServer: "https://registry.example.com/npm/"
npmPublishRegistry: "https://registry.example.com/npm/"
npmAuthToken: "${NPM_TOKEN}"

Yarn 4.15 and later wait a day before installing a newly published version. On Yarn 4.17 and later, add this to your scope’s block to install your own packages the day you publish them. On 4.15 or 4.16, update Yarn or install the next day.

npmScopes:
yourscope:
npmRegistryServer: "https://registry.example.com/npm/"
npmPublishRegistry: "https://registry.example.com/npm/"
npmAuthToken: "${NPM_TOKEN}"
npmMinimalAgeGate: 0

This lets Yarn install versions you published today from your own repository. Delete that line to keep Yarn’s one-day delay.

This is where CloudRepo helps

An npm group in CloudRepo (early access) puts your hosted npm repository and a proxy of npmjs behind one URL, so a single registry= line serves both with no scope mapping to keep in sync. A package name your hosted repository holds is served only from there, never from npmjs, and public packages are cached in your proxy the first time a build asks for them.

registry=https://your-org.mycloudrepo.io/repositories/npm-group/
//your-org.mycloudrepo.io/repositories/npm-group/:_authToken=${NPM_TOKEN}

More: Install npm packages from CloudRepo, npm groups, Proxy repositories, Repository tokens.

Every feature on every plan, with unlimited users

14 days free. No credit card required.

Starting at $99/month billed annually.

Chris Shellenbarger, founder of CloudRepo

Founder-run since 2016, with no venture capital. A real person answers within one business day. Usually it's me.

Chris, founder of CloudRepo