Maaz Zindani

Loading the index…

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

npm publish to the GitLab registry says you must be logged in

Errornpm error code ENEEDAUTH

Short 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.

  • Applies to npm 10 and 11, Node.js 22, GitLab package registry, GitLab CI
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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-run
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The 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. 3Both 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

What does the error look like?

  • npm error code ENEEDAUTH
  • This 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:registry

2Fix

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-run

Confirmed by docs.gitlab.com

Did this fix it?

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 publish

3Verify

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-run

Confirmed by docs.gitlab.com

Did this fix it?

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-run

Confirmed by docs.npmjs.com

Did this fix it?

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

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 fix

Back 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

Next step

Want this handled for good?

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