npm publish to the GitLab registry says you must be logged in
npm error code ENEEDAUTHShort answer
npm picked a registry for your package name, looked for a token for that exact address, and found none. The URL in the error shows which registry it chose. Either the package scope is not mapped to your GitLab project, the job's .npmrc has no token line, or the token line's address differs from the registry line.
Fix it fast
1Find out which cause you have
Run a dry run. With --dry-run npm prints the same message as a warning, and the address in it is the registry npm chose for this package name. Compare that address with your GitLab project's registry URL.
grep '"name"' package.json npm publish --dry-run2Match what you see, then jump to its fix
- 1
The address in the message is https://registry.npmjs.org/ and not your GitLab URLThe package scope is not mapped to the GitLab registry, so npm targets the default oneFix - 2
The address in the message is your GitLab URL, and no ':_authToken' line for it exists in the project .npmrc or in ~/.npmrcThe .npmrc that npm reads has no token line for that registryFix - 3
Both lines exist, but the host, the project ID or the endpoint type (instance or project) differs between themThe token line is for a different address than the registry lineFix
- 1
What does the error look like?
npm error code ENEEDAUTHThis command requires you to be logged in to https://gitlab.example.com/api/v4/projects/123/packages/npm/
Cause 1 of 3
The package scope is not mapped to the GitLab registry, so npm targets the default one
1Check
npm chooses the registry from the package name. A scoped name uses the registry set for that scope; with no mapping it falls back to the default registry. GitLab's guide says the scope in package.json and the @scope:registry line in .npmrc must agree.
grep '"name"' package.json
npm config get @my-scope:registry2Fix
Map the scope to your project's registry in .npmrc, or put the same mapping under publishConfig in package.json. Use the same scope in both places and start the package name with it. GitLab's convention is the project's top-level namespace in lowercase, though the project endpoint does not enforce it.
# .npmrc
@my-scope:registry=https://gitlab.example.com/api/v4/projects/<project_id>/packages/npm/
// package.json (alternative)
"publishConfig": {
"@my-scope:registry": "https://gitlab.example.com/api/v4/projects/<project_id>/packages/npm/"
}3Verify
The dry run should now name your GitLab address in its message instead of registry.npmjs.org.
npm publish --dry-runConfirmed by docs.gitlab.com
Cause 2 of 3
The .npmrc that npm reads has no token line for that registry
1Check
npm looks for credentials registered for the chosen registry address. If none is found, the publish stops before sending anything. In a CI job, the runner starts from a clean checkout, so nothing from your laptop's ~/.npmrc is there. This command lists the token lines without printing the tokens.
grep -n ':_authToken' .npmrc ~/.npmrc 2>/dev/null | sed 's/=.*/=<hidden>/'2Fix
In the GitLab job, write both lines into .npmrc before npm publish, using the job token. Do not add a port after the host when it is the default for the protocol. Locally, use a token with the api scope, or a deploy token that can write to the package registry, and keep it in an environment variable, not in a committed file.
publish-npm:
stage: deploy
script:
- echo "@my-scope:registry=https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/" > .npmrc
- echo "//${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}" >> .npmrc
- npm publish3Verify
Run the job again. A dry run placed before the real publish should no longer warn about being logged in, and the real publish should finish.
npm publish --dry-runConfirmed by docs.gitlab.com
Cause 3 of 3
The token line is for a different address than the registry line
1Check
Credentials in .npmrc are tied to a registry address that starts with // and are matched by walking up the path of the registry URL. A token written for the instance endpoint does not belong to a project endpoint, and a different project ID is a different address. Put the two lines side by side and compare them character by character.
grep -n -E '(registry|:_authToken)' .npmrc | sed -E 's/(_authToken=).*/\1<hidden>/'2Fix
Make the part after // in the token line the same address as the registry line. If you publish to a project endpoint, the token line must name that same project ID and not the instance endpoint.
@my-scope:registry=https://gitlab.example.com/api/v4/projects/123/packages/npm/
//gitlab.example.com/api/v4/projects/123/packages/npm/:_authToken=${NPM_TOKEN}3Verify
Run the dry run and then publish; the message about being logged in should not come back.
NPM_TOKEN=<token> npm publish --dry-runConfirmed by docs.npmjs.com
How do I stop it coming back?
- Keep one .npmrc in the repository with the scope mapping, and let the CI job add only the token line from CI_JOB_TOKEN.
- Never commit a token; reference an environment variable or the job token instead.
- Put the registry mapping in publishConfig, so a publish from a laptop goes to GitLab and not to the public registry.
- Run npm publish --dry-run as a pipeline step on merge requests, so the login problem shows before a release.
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.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
npm install returns 401 for a private package on GitLab
npm ERR! 401 Unauthorized
GitLab answers 401 when npm sends no token or one it does not accept.
3 causes, each with a fixBack end
npm publish fails in GitHub Actions with ENEEDAUTH
npm error code ENEEDAUTH
ENEEDAUTH means npm has no valid login in the job.
4 causes, each with a fixBack end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fix