Yarn 4 gets 401 from a private registry that npm can still reach
Fix it fast
Most common cause, 1 of 3The token lives only in .npmrc, which Yarn Modern ignores
Write the registry and token into the project .yarnrc.yml.
npmScopes:
my-company:
npmRegistryServer: "https://npm.example.com"
npmAuthToken: "${NPM_TOKEN}"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.
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).
yarn --version
yarn config get npmScopes
yarn npm whoami --scope my-companyThen find the line you see in this list and open its fix:
- 1The token lives only in .npmrc, which Yarn Modern ignores
yarn --version prints 2.x or newer, .npmrc has an _authToken line, and the error says as an anonymous user - 2The token is set, but not for the registry the package comes from
npmAuthToken exists at the top level, yet the failing package belongs to a scope that npmScopes sends to another registry - 3A login made on one machine, or an expired token, used in CI
Works 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.
grep -n "_authToken" .npmrc ~/.npmrc 2>/dev/null; grep -n "npmAuthToken" .yarnrc.yml ~/.yarnrc.yml 2>/dev/nullFix
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.
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.
NPM_TOKEN=<token> yarn installConfirmed by yarnpkg.com
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.
yarn config get npmScopes --json
yarn config get npmRegistries --jsonFix
Put npmAuthToken next to the npmRegistryServer of the scope that fails, or key it by the registry host under npmRegistries.
npmRegistries:
"//npm.pkg.github.com":
npmAuthToken: "${GITHUB_TOKEN}"Verify
The scope-specific whoami succeeds and the package downloads.
yarn npm whoami --scope my-companyConfirmed by yarnpkg.com
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.
yarn config get npmAuthToken --whyFix
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.
# CI job
export NPM_TOKEN="$REGISTRY_TOKEN_SECRET"
yarn install --immutableVerify
The CI log shows the install step finishing, and yarn npm whoami in the job prints the bot account.
yarn npm whoami --scope my-companyConfirmed by yarnpkg.com
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.