npm install returns 401 Unauthorized - Gitlab NPM registry (private)

Viewed 2457

I'm trying to use a npm package stored at Gitlab npm registry at a private project.

To publish my package i used:

  • My .gitlab-ci.yml:

    image: node:12
    stages:
      - deploy
    deploy:
      stage: deploy
      script:
        npm config set @{{my-org-name}}:registry https://gitlab.com/api/v4/projects/{{project-id}}/packages/npm/
        npm config set "//gitlab.com/api/v4/projects/{{project-id}}/packages/npm/:_authToken" '{{my-auth-token}}'
        npm publish
    
  • Then the publish config at package.json:

    "publishConfig": {
      "access": "public",
      "@{{my-org-name}}:{{my-package-name}}": "https://gitlab.com/api/v4/projects/{{project-id}}/packages/npm/"
    }
    

This steps creates the package correctly, but when trying to install with npm i @{{my-org-name}}/{{my-package-name}} I step with 401 Unauthorized error.

I assume I'm missing an auth config somewhere, but can't figure out which or where!

1 Answers

I am working in a private module, and the only way I have been able to install the package is by setting .npmrc in the root of the project where I want to install the module with content like this:

# Set URL for scoped packages.
@{scope}:registry=https://gitlab.yourdomain.com/api/v4/packages/npm/

# Create an environment variable for your GitLab auth token
'//gitlab.yourdomain.com/api/v4/packages/npm/:_authToken'="${GITLAB_AUTH_TOKEN}"

'//gitlab.yourdomain.com/api/v4/projects/{PROJECT_ID}/packages/npm/:_authToken'="${GITLAB_AUTH_TOKEN}"

GITLAB_AUTH_TOKEN is a personal token that I had to create on GitLab, under Profile and Access Tokens. I put it in an environment variable.

However, I want to find a way authenticate users without needing to create a personal token; I want people in the company to be able to install the module in an another way, maybe using their GitLab credentials, but I have not seen anything like that...

Related