gitlab npm registry failing

79 Views Asked by At

I'm trying to publish a test for a npm package but it constantly fails. In my package.json I have named the package after "@username/package_name" and also configured the publishConfig object:

"publishConfig": {
    "@username:registry": "https://gitlab.com/api/v4/projects/<projectID>/packages/npm"
  },

My CICD YAML file is configured this way:

image: node:latest

stages:
  - publish

publish:
  stage: publish

  before_script:
    - echo "@username:registry=http://${CI_SERVER_HOST}:${CI_SERVER_PORT}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/">.npmrc
    - echo "//${CI_SERVER_HOST}:${CI_SERVER_PORT}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}">>.npmrc

  script:
    - npm publish

and it fails with

npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in to https://gitlab.com/api/v4/projects/<projectID>/packages/npm

Any thoughts on what's wrong?

I've read a lot, tried to downgrade node version to 20, 18 and even 16. I followed tutorials and Gitlab documentation. Nothing seems to work.

1

There are 1 best solutions below

1
On

As mentioned in the docs don't specify CI_SERVER_PORT if it's a default port:

When generating the .npmrc file, do not specify the port after ${CI_SERVER_HOST} if it is a default port, such as 80 for a URL starting with http or 443 for a URL starting with https.

So, you should use something like this:

  script:
    - echo "@username: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