gitlab npm publish not working: This command requires you to be logged in
Asked Answered
C

4

6

I am new to this gitlab world.

I have one private project. I am able to publish the package using command prompt.

My .npmrc file

@ui-practice:registry=https://gitlab.MyCompany.com/api/v4/packages/npm/
//gitlab.MyCompany.com/api/v4/packages/npm/:_authToken=PrivateToken
//gitlab.MyCompany.com/api/v4/projects/MyProjectID/packages/npm/:_authToken=PrivateToken

But I am unable to publish using Gitlab CICD, it giving below error

npm ERR! need auth This command requires you to be logged in to https://gitlab.MyCompany.com/api/v4/projects/MyProjectID/packages/npm/
npm ERR! need auth You need to authorize this machine using `npm adduser`
npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Windows\system32\config\systemprofile\AppData\Local\npm-cache\_logs\2022

my .gitlab-ci.yml

image: node:latest


stages:
  - deploy

deploy:
  stage: deploy
  script:
    - echo "@MyScope:registry=https://${CI_SERVER_HOST}/api/v4/packages/npm/">.npmrc
    - echo "//${CI_SERVER_HOST}/api/v4/packages/npm/:_authToken=${CI_JOB_TOKEN}">>.npmrc
    - echo "//${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}">>.npmrc
    - echo "Created the following .npmrc:"; cat .npmrc
    - npm publish

CI_JOB_TOKEN - I have created deploy token and added in gitlab variables

My package.json

{
  "name": "@MyScope/MyPackageName",
  "version": "1.0.23",
  "description": "My Library.",
  "publishConfig": {
    "@MyScope:registry": "https://gitlab.MyCompany.com/api/v4/projects/MyProjectID/packages/npm/"
  },
  "scripts": {
    "test": "web-test-runner \"./stories/**/*.test.js\" --node-resolve",
    "storybook": "start-storybook -p 6006",
    "build-storybook": "build-storybook",
    "chromatic": "npx chromatic --project-token=78290d9f7c4f",
    "build": "rimraf build && rollup -c",
    "prepublishOnly": "npm install && npm run build",
    "pack:dev": "npm pack && move *.tgz node_modules_offline/"
  },
  "repository": {
    "type": "git",
    "url": "https://gitlab.MyCompany.com/MyScope/MyPackageName.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@applitools/eyes-storybook": "^3.27.6",
    "@babel/core": "^7.17.7",
    "@babel/plugin-proposal-class-properties": "^7.16.7",
    "@babel/plugin-proposal-decorators": "^7.17.2",
    "@babel/plugin-proposal-export-default-from": "^7.16.7",
    "@babel/plugin-proposal-nullish-coalescing-operator": "^7.16.7",
    "@babel/plugin-proposal-object-rest-spread": "^7.17.3",
    "@babel/plugin-proposal-optional-chaining": "^7.16.7",
    "@babel/plugin-proposal-private-methods": "^7.16.11",
    "@babel/plugin-syntax-dynamic-import": "^7.8.3",
    "@babel/plugin-transform-arrow-functions": "^7.16.7",
    "@babel/plugin-transform-block-scoping": "^7.16.7",
    "@babel/plugin-transform-classes": "^7.16.7",
    "@babel/plugin-transform-destructuring": "^7.17.7",
    "@babel/plugin-transform-for-of": "^7.16.7",
    "@babel/plugin-transform-parameters": "^7.16.7",
    "@babel/plugin-transform-runtime": "^7.17.0",
    "@babel/plugin-transform-shorthand-properties": "^7.16.7",
    "@babel/plugin-transform-spread": "^7.16.7",
    "@babel/preset-env": "^7.16.11",
    "@babel/preset-typescript": "^7.16.7",
    "@rollup/plugin-babel": "^5.3.1",
    "@rollup/plugin-commonjs": "^21.1.0",
    "@rollup/plugin-node-resolve": "^13.2.1",
    "@rollup/plugin-strip": "^2.1.0",
    "@rollup/plugin-typescript": "^8.3.2",
    "@storybook/addon-a11y": "^6.4.19",
    "@storybook/addon-actions": "^6.4.19",
    "@storybook/addon-essentials": "^6.4.19",
    "@storybook/addon-links": "^6.4.19",
    "@storybook/addons": "^6.4.19",
    "@storybook/preset-scss": "^1.0.3",
    "@storybook/theming": "^6.4.19",
    "@storybook/web-components": "^6.4.19",
    "babel-loader": "^8.2.3",
    "babel-plugin-macros": "^3.1.0",
    "babel-plugin-polyfill-corejs3": "^0.5.2",
    "chromatic": "^6.5.4",
    "core-js": "^3.21.1",
    "css-loader": "^5.2.7",
    "eslint": "^8.11.0",
    "html-minifier-terser": "^7.0.0-alpha.2",
    "rimraf": "^3.0.2",
    "rollup": "^2.70.2",
    "rollup-plugin-copy": "^3.4.0",
    "rollup-plugin-dts": "^4.2.1",
    "rollup-plugin-minify-html-literals": "^1.2.6",
    "rollup-plugin-peer-deps-external": "^2.2.4",
    "rollup-plugin-scss": "^3.0.0",
    "rollup-plugin-terser": "^7.0.2",
    "sass": "^1.49.9",
    "sass-loader": "^10.2.1",
    "storybook-dark-mode": "^1.0.9",
    "style-loader": "^2.0.0"
  },
  "dependencies": {
    "@esm-bundle/chai": "^4.3.4-fix.0",
    "i18next": "^21.6.14",
    "lit-html": "^2.2.0",
    "node-sass": "^6.0.1"
  },
  "main": "build/cjs/index",
  "module": "build/esm/index",
  "files": [
    "build"
  ]
}

what is missing? any idea why gitlab asked me login again?

need auth This command requires you to be logged in to

It's able to build the artifacts only issue in publishing the package using CICD, from visual studio terminal it's working fine.

Cudbear answered 21/4, 2022 at 10:41 Comment(1)
I had the same problem, see my answer https://mcmap.net/q/699709/-how-do-i-publish-a-private-npm-package-with-gitlab-ciDocila
K
4

i had the same problem and the reason it doesn't work for both of us is related to the registry endpoint we were using. Both of us are using instance level endpoint, that can't be used to publish anything.

This is your .npmrc which contains, like mine, a instance level registry

@MyScope:registry=https://gitlab.MyCompany.com/api/v4/packages/npm/
//gitlab.MyCompany.com/api/v4/packages/npm/:_authToken=PrivateToken
//gitlab.MyCompany.com/api/v4/projects/1233/packages/npm/:_authToken=PrivateToken

This is your extract from .gitlab-ci.yml

  script:
- echo "@MyScope:registry=https://${CI_SERVER_HOST}/api/v4/packages/npm/">.npmrc
- echo "//${CI_SERVER_HOST}/api/v4/packages/npm/:_authToken=${CI_JOB_TOKEN}">>.npmrc
- echo "//${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}">>.npmrc
- echo "Created the following .npmrc:"; cat .npmrc
- npm publish

My solution was change the first echo to:

  - echo "@MyScope:registry=https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/">.npmrc

This switched from instance level package to project level package where you can actually publish your package.

Kibosh answered 6/7, 2022 at 22:58 Comment(0)
J
4

I had the same issue and I could fix it with the right url in package.json. Hope this helps.

"publishConfig": {
    "@company:registry": "https://gitlab.com/api/v4/projects/project_id/packages/npm/"
  },
Jigaboo answered 25/10, 2022 at 20:1 Comment(0)
E
0

Compare the output of echo "Created the following .npmrc:"; cat .npmrc to your local .npmrc file. Are there any discrepancies? Is it possible that one or more of the environment variables within CI are blank?

Eddi answered 21/4, 2022 at 14:40 Comment(2)
Can you please suggest?Cudbear
both the .npmrc files content are sameCudbear
C
-1

Thank you for your response, Both are same

My local .npmrc file

@MyScope:registry=https://gitlab.MyCompany.com/api/v4/packages/npm/
//gitlab.MyCompany.com/api/v4/packages/npm/:_authToken=PrivateToken
//gitlab.MyCompany.com/api/v4/projects/1233/packages/npm/:_authToken=PrivateToken

And out put from .yml file i.e. cat .npmrc

Created the following .npmrc:

@MyScope:registry=https://gitlab.MyCompany.com/api/v4/packages/npm/
//gitlab.MyCompany.com/api/v4/packages/npm/:_authToken=PrivateToken
//gitlab.MyCompany.com/api/v4/projects/1233/packages/npm/:_authToken=PrivateToken
Cudbear answered 22/4, 2022 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.