Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Yarn 4 gets 401 from a private registry that npm can still reach

Your error

Invalid authentication (as an anonymous user)1 more line it can show

Fix it fast

Most common cause, 1 of 3

The token lives only in .npmrc, which Yarn Modern ignores

Write the registry and token into the project .yarnrc.yml.

Fix
npmScopes:
  my-company:
    npmRegistryServer: "https://npm.example.com"
    npmAuthToken: "${NPM_TOKEN}"
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Yarn 2 and later (Yarn 4.18 today) never read .npmrc, so a token that works for npm is invisible to yarn install and the private registry answers 401, shown as YN0041 Invalid authentication. Move the token into .yarnrc.yml as npmAuthToken, under npmScopes or npmRegistries for that registry, ideally read from an environment variable.

  • Applies to Yarn 4.18.1, Yarn 2 and 3 (.yarnrc.yml)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Check the Yarn major version, then ask Yarn who it is for the scope that fails. "anonymous user" in the error means Yarn sent no token at all (causes 1 and 2); "unknown user" means it sent one that the registry refused (cause 3).

Run this first
yarn --version
yarn config get npmScopes
yarn npm whoami --scope my-company

Then find the line you see in this list and open its fix:

  1. 1The token lives only in .npmrc, which Yarn Modern ignoresyarn --version prints 2.x or newer, .npmrc has an _authToken line, and the error says as an anonymous user
  2. 2The token is set, but not for the registry the package comes fromnpmAuthToken exists at the top level, yet the failing package belongs to a scope that npmScopes sends to another registry
  3. 3A login made on one machine, or an expired token, used in CIWorks on your laptop after yarn npm login, fails in CI; or the error says as an unknown user

What does the error look like?

  • Invalid authentication (as an anonymous user)
  • Invalid authentication (as an unknown user)

Cause 1 of 3

The token lives only in .npmrc, which Yarn Modern ignores

You seeyarn --version prints 2.x or newer, .npmrc has an _authToken line, and the error says as an anonymous user

Check

Yarn 2+ answers a private registry with 401 when the token sits only in .npmrc. The Yarn migration guide says Modern uses .yarnrc.yml and ignores every other file, .npmrc included, and that registry tokens are now set with npmAuthToken. npm keeps working on the same machine, which hides the problem.

Check
grep -n "_authToken" .npmrc ~/.npmrc 2>/dev/null; grep -n "npmAuthToken" .yarnrc.yml ~/.yarnrc.yml 2>/dev/null

Fix

Write the registry and token into the project .yarnrc.yml. Use environment variable expansion so the secret stays out of git; ${NPM_TOKEN} makes Yarn stop with an error when the variable is missing.

Fix
npmScopes:
  my-company:
    npmRegistryServer: "https://npm.example.com"
    npmAuthToken: "${NPM_TOKEN}"

Verify

yarn npm whoami --scope my-company prints your registry username, and yarn install completes.

Verify
NPM_TOKEN=<token> yarn install

Confirmed by yarnpkg.com

Did this fix it?

Cause 2 of 3

The token is set, but not for the registry the package comes from

You seenpmAuthToken exists at the top level, yet the failing package belongs to a scope that npmScopes sends to another registry

Check

Yarn picks credentials per registry. A top-level npmAuthToken is the default, but a scope listed under npmScopes uses its own npmRegistryServer and its own auth settings, and npmRegistries can override credentials per registry URL. A token written at the wrong level is simply not sent to the host that needs it.

Check
yarn config get npmScopes --json
yarn config get npmRegistries --json

Fix

Put npmAuthToken next to the npmRegistryServer of the scope that fails, or key it by the registry host under npmRegistries.

Fix
npmRegistries:
  "//npm.pkg.github.com":
    npmAuthToken: "${GITHUB_TOKEN}"

Verify

The scope-specific whoami succeeds and the package downloads.

Verify
yarn npm whoami --scope my-company

Confirmed by yarnpkg.com

Did this fix it?

Cause 3 of 3

A login made on one machine, or an expired token, used in CI

You seeWorks on your laptop after yarn npm login, fails in CI; or the error says as an unknown user

Check

yarn npm login stores the new token in the configuration in your home folder, never in the project, so a CI runner or a teammate does not get it. When the error says unknown user, Yarn did send a token and the registry rejected it, which points at an expired, revoked or wrong-scope token rather than missing config.

Check
yarn config get npmAuthToken --why

Fix

Create a fresh token in the registry, store it as a CI secret, and expose it to the job as the variable your .yarnrc.yml expands. Yarn's own CI example sets the token with yarn config set inside the job.

Fix
# CI job
export NPM_TOKEN="$REGISTRY_TOKEN_SECRET"
yarn install --immutable

Verify

The CI log shows the install step finishing, and yarn npm whoami in the job prints the bot account.

Verify
yarn npm whoami --scope my-company

Confirmed by yarnpkg.com

Did this fix it?

How do I stop it coming back?

  • Commit a .yarnrc.yml that names every private scope and reads each token from an environment variable, and delete the old .npmrc once the project uses Yarn 2+.
  • Run yarn npm whoami --scope <scope> as the first CI step so an expired token fails fast with a clear message.
  • Rotate registry tokens on a schedule and record the expiry date next to the CI secret.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.