How to upload an NPM package to GitLab's package registry?

Trouble with GitLab NPM package registry

I’m trying to upload my NPM package to GitLab’s package registry but I’m running into some issues. Here’s what I’ve got in my .gitlab-ci.yml:

image: node:18

stages:
  - deploy

deploy_package:
  stage: deploy
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  script:
    - echo "//${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}">.npmrc
    - npm publish

When I run this, I get an error saying I need to log in to npmjs.org. But I want to use GitLab’s registry, not npm’s. I’ve tried a few things:

  1. Added a registry line to .npmrc
  2. Switched to node:14 (which worked, oddly enough)
  3. Changed the script to use more specific publish commands

Nothing seems to work right. Either I get auth errors or 404 errors. I’m out of ideas. How can I get my package into GitLab’s registry? Any help would be great!

ey mate, had similar issues. try adding this to ur package.json:

"publishConfig": {
  "@your-namespace:registry": "https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/"
}

replace ‘your-namespace’ with ur actual namespace. this should point npm to the right registry. hope it helps!

Hey there! I’ve gone through this exact headache before. Here’s what finally worked for me:

Make sure your package.json has the right publishConfig. It should look something like this:

“publishConfig”: {
@your-group-name:registry”: “https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/”
}

Replace ‘your-group-name’ with your actual group name.

Then, in your CI script, add these lines before npm publish:

npm config set //${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken ${CI_JOB_TOKEN}
npm config set @your-group-name:registry https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/

This sets up the auth and registry correctly for your publish command.

Also, double-check that your project settings have the package registry enabled. It’s easy to miss!

Hope this helps you get your package published. Let me know if you hit any other snags!

I’ve encountered similar issues when setting up GitLab’s NPM registry. Here’s what worked for me:

First, ensure your .npmrc file is correctly configured. Add this line:

@${CI_PROJECT_PATH}:registry=https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/

Then, modify your package.json to include:

“publishConfig”: {
“@${CI_PROJECT_PATH}:registry”: “https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/”
}

Finally, in your CI script, run:

npm config set //${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken ${CI_JOB_TOKEN}

npm publish

This approach should resolve authentication issues and ensure you’re publishing to the correct registry. Let me know if you need further clarification.