Skip to content

Fix npm E401: private registry authentication

npm E401 means that the registry did not accept the credential of a request. Examine the registry host, the token variable and the key in that sequence.

Overview

npm reports E401 when a registry answers a request with HTTP 401 Unauthorized. On PrivateNPM, 401 means that the request had no credential, or that the registry did not accept the credential. This guide gives a sequence of checks that finds the cause. The guide is for developers and CI maintainers who install or publish private packages.

The output of the failed command contains lines similar to these. The exact text changes with the npm version:

npm error code E401
npm error 401 Unauthorized - GET https://registry.privatenpm.com/@acme%2fsdk

Other forms of the same error are Unable to authenticate and authentication token not provided. A valid key can be absent from the request. The key can be on a line for a different host, or the variable can be absent from the build environment. Thus examine the request first, and then the key.

  1. Match the registry and the credential host of the failed request.
  2. Make sure that the variable is present in the same shell or CI step.
  3. Use the rules of your package manager. npm and pnpm read project files differently.
  4. Use the correct key for the operation, and examine the status of that key.

Before you start

Make sure that you have these items:

  • The command that failed, and the directory where it ran.
  • Access to the same shell, or to the CI log and the CI secret store.
  • The registry URL. The shared registry is https://registry.privatenpm.com/.
  • For a private registry, its active custom domain, for example https://npm.acme.com/.
  • A package that the key can read, for example @acme/sdk.

PrivateNPM accepts only its own keys. Download keys start with pdk_ and publisher keys start with prt_. A key from npmjs.org or GitHub Packages does not authenticate to PrivateNPM.

How it works

How npm attaches a credential

For each request, the npm CLI compares the request URL with the :_authToken lines of its configuration. If the host and path of a line match the URL, npm sends the value in the Authorization: Bearer header. If no line matches, npm sends the request without a credential. Thus a correct key under the wrong host has the same result as no key.

An install sends two types of request. The first request asks for the packument, the document that lists the versions, the dist-tags and the tarball URLs. The second request downloads the tarball of the selected version. Each request carries the credential again, and the registry authorizes each request again.

How the registry checks a request

The registry does not accept anonymous requests. It examines each request in this sequence:

  1. The header must contain Bearer and a value that starts with prt_ or pdk_. Else the registry returns 401.
  2. The key must exist, and it must not be expired or revoked. Else the registry returns 401.
  3. A download key of a suspended recipient also returns 401.
  4. The key must be for this registry. Else the registry returns 403.
  5. The package must exist in this registry and belong to the organization of the key. Else the registry returns 404.
  6. The key must permit the operation on the package. Else the registry returns 403.

Thus 401 is about the credential, 403 is about permissions, and 404 is about the name. The registry returns 404, not 403, for a package of a different organization. Then a key cannot find the private package names of other organizations.

A 403 response body contains a reason code. The Access denied tab under Registry → Activity shows the same reason for each refused request. A 401 does not appear on that tab, because the registry cannot connect the request to an organization.

Reason codeCause
registry_not_in_token_audienceThe key is for a different registry.
token_deniedThe publisher key does not have the action, for example Publish or Read tarballs.
permission_deniedThe member who owns the publisher key does not have the permission in the organization now.
package_outside_token_restrictionsThe restrictions of the publisher key do not include this scope or package.
not_entitledThe download key does not grant this package.
download_key_cannot_publishA download key tried a write operation.

1. Match the registry and the credential host

Run these commands in the directory where the failed command runs:

npm config get @acme:registry
npm config get registry
npm config get userconfig

For this example, the scope route must show https://registry.privatenpm.com/. If the scope route is undefined, npm uses the default registry, and the request goes to npmjs.org. The userconfig result shows the active user file. CI can move that file to a temporary location.

For npm, this project .npmrc sends the scope and its credential to the same host:

@acme:registry=https://registry.privatenpm.com/
//registry.privatenpm.com/:_authToken=${ACME_NPM_TOKEN}

A credential for registry.npmjs.org does not go with a request to registry.privatenpm.com. For a custom domain, change both lines to the active custom domain. If the registry URL has a port, put the same port in the credential line. If the registry URL has a path, put the same path in the credential line.

Show the effective configuration with this command:

npm config list

The output shows (protected) in place of each _authToken value, so you can share it. Command-line flags and npm_config_* environment variables can replace values from files. See the npm configuration rules.

To see each request, add --loglevel http to the failed command. npm prints the method, the status and the URL of each request. Compare the host of the failed URL with the host of each credential line.

If the packument request passes but a tarball request fails, examine the tarball URL. The lockfile can record tarball URLs on an old host. A different host needs its own credential line, or a lockfile that you generate again.

2. Make sure that the variable is present

A reference to ${ACME_NPM_TOKEN} does not create the variable. Supply a download key through your shell or your CI secret store. Then run this command in the same process environment as the install:

node -e 'if (!process.env.ACME_NPM_TOKEN) { console.error("ACME_NPM_TOKEN is missing or empty"); process.exit(1); } console.log("ACME_NPM_TOKEN is set");'

The result ACME_NPM_TOKEN is set proves only that a value exists. The result does not prove that the key is valid. If the check fails, correct the name of the variable or the place where you set it. If the variable is not set, npm does not send a usable credential, and the registry returns 401.

npm does not read .env files. A value that is only in .env is not in the environment of npm. Export the value in the shell, or use a tool that exports it before npm starts.

In CI, attach the secret to the step that installs dependencies. Examine the environment restrictions of the secret. By default, GitHub does not give repository secrets to a workflow that a pull request from a fork starts. Thus a local success does not prove that a pull request build has the same credentials. With actions/setup-node, use the NODE_AUTH_TOKEN variable as the GitHub Actions configuration shows.

WARNING: Do not print the value of a key in diagnostic output. A CI log can keep the value, and anyone who reads the log can use the key.

3. Use the rules of your package manager

For pnpm 12, keep the scope route in the project .npmrc. Remove a credential line that refers to an environment variable:

@acme:registry=https://registry.privatenpm.com/

Since pnpm 11.5.3, pnpm ignores environment references in the authentication values of a project file. Supply ACME_NPM_TOKEN from your secret store. Then give pnpm the credential through a host-scoped environment variable in a POSIX shell:

env "pnpm_config_//registry.privatenpm.com/:_authToken=$ACME_NPM_TOKEN" pnpm view @acme/sdk version

The command prints the published version of the package. A user-level authentication file can still contain the reference. See the pnpm authentication documentation.

Current Yarn releases do not read .npmrc. Yarn reads the registry and the credential from npmScopes in .yarnrc.yml. The .npmrc guide shows the pnpm and Yarn files.

4. Use the correct key for the operation

If npm ci or an install fails, use a download key. The key must grant each private dependency that the install needs. If npm publish fails, use a publisher key that has the Publish action for that package. Also examine publishConfig.registry in package.json, because that value replaces the scope route for a publish.

A download key cannot publish. Open Registry → Registry keys to examine each key. The list shows the package access, the permissions, the last use and the status of each key. Last used changes after a delay of some seconds. If the value does not change after one minute, the registry did not receive a valid request with that key.

A publisher key acts with the current permissions of its member. The key cannot do more than the role of the member. For example, the built-in Member role can publish but cannot unpublish. If the member leaves the organization, the publisher key stops.

Trusted publishing with OIDC authorizes a publish from CI. It does not authenticate the install of private dependencies before the publish. Supply a download key for that install. For a refused OIDC exchange, use the checks in that guide.

Verify the fix

Before you start, set the variable in the shell or the CI step that failed. Use a package that the key can read.

  1. Make sure that the registry accepts the credential:
    npm whoami --registry=https://registry.privatenpm.com/
    For a download key, npm prints download: and the key ID. For a publisher key, npm prints the email address of the member.
  2. Request the packument of a package that the key can read:
    npm view @acme/sdk version
    npm prints the version.
  3. Run the original install from a clean checkout. Use the committed lockfile, the same key and an empty cache:
    npm ci --cache "$(mktemp -d)"
    npm installs the dependencies without an error.

Step 2 proves only that the metadata request works. Step 3 also proves tarball access. Keep your usual policies for dependency release age and lifecycle scripts. An authentication correction does not need weaker policies.

For pnpm, apply the credential to the install command too:

env "pnpm_config_//registry.privatenpm.com/:_authToken=$ACME_NPM_TOKEN" pnpm install --frozen-lockfile --ignore-scripts

If you test a change to a download key, use an empty temporary store. A cached copy can hide a refused download. To verify a publish, follow the publish guide with a new version of a package that you control.

Troubleshoot other results

ResultCauseCorrection
E401 only in CIThe secret is not attached to the install step, or the run cannot receive secrets.Attach the secret to the step. Examine the rules for pull requests from forks.
E401 after a key changeSomeone rotated or revoked the key, or the key expired. A suspended recipient has the same result.Obtain the current key value and update each secret store.
E403 on installThe key is valid, but it does not grant the package or the operation.Read the reason code. Change the package access of the key, or use a different key.
E403 on publishThe version exists, the scope is not reserved, the plan limit applies, or the key cannot publish the package.Read the message in the output. The publish guide explains each message.
E404The scope route is absent, the name is wrong, or the version does not exist.Examine the route and the name. A package of a different organization also returns 404.
Unknown user config "always-auth"An obsolete option is in a configuration file.Remove the line as the always-auth procedure shows.

For the full npm, pnpm, Yarn and CI configuration, use the private registry .npmrc guide.

Security and maintenance

  • Do not paste a full .npmrc file, a full environment or a key into an issue or a chat.
  • If a key appeared in a log or a screenshot, rotate the key. The old secret stops after the rotation.
  • Give each pipeline and each recipient a separate key. Then one revocation does not stop the other builds.
  • Set an expiry date on keys for temporary access. An expired key returns 401.
  • Examine the Access denied tab after a change of key access. It shows the reason for each refused request.
  • Examine Organization → Audit log to find who issued, rotated or revoked a key.

READY WHEN YOU ARE

Put private packages to work.