How do I publish a private npm package with gitlab ci? - npm

I want to publish a private npm package with Gitlab CI.
I've created an auth token for my npm user and set it as a variable NPM_TOKEN in my Gitlab CI settings.
The job then creates an .npmrc file with the registry and the auth token.
- npm run build && npm run build:es6
- echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}'>.npmrc
- npm publish
The job fails with this message:
npm ERR! code ENEEDAUTH
npm ERR! need auth auth required for publishing
npm ERR! need auth You need to authorize this machine using `npm adduser`
Is it possible to publish with only an auth token?

As #Amityo said, rather than manually editing the npmrc file,
npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN}
is the way to go, because otherwise you may be editing the wrong npmrc file.
If you are still getting an authentication error, and are certain that the token is correct, check your registry URL. You can run
npm publish --verbose
whose output will includes lines like
npm verb getPublishConfig { registry: 'https://.......' }
npm verb mapToRegistry no registry URL found in name for scope #boxine
npm verb publish registryBase https://.......
If you are publishing to npmjs.org, the URL (....... above) should be https://registry.npmjs.org/ .
If this registry URL does not fit, look in your npmrc file for a different one. Also make sure you didn't override the registry in your package.json file! You can search for publishConfig in that file.

To elaborate slightly on #phihag's answer, forward slashes are very important.
At first I kept getting 404 not found
$ npm publish
...
...
npm ERR! code E404
npm ERR! 404 Not Found - PUT https://gitlab.company.com/api/v4/packages/npm/%2fmypackage - 404 Not Found
npm ERR! 404
npm ERR! 404 '#scope/mypackage#0.1.0' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
I am using 2FA so as the gitlab docs state, I need to use a personal access token set to api to authenticate. In another part of the gitlab docs it states
Some features such as publishing a package is only available on the project-level endpoint.
So in console I tried to publish to and authenticate at the project level
$ npm config set #scope:registry https://gitlab.company.com/api/v4/projects/123/packages/npm
$ npm config set //gitlab.company.com/api/v4/projects/123/packages/npm:_authToken 'MyGeneratedAccessToken'
Which eliminated my first issue of 404 not found, but now I couldn't authenticate.
For hours.
$ npm publish --verbose
npm verb cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'publish', '--verbose' ]
npm info using npm#7.11.2
npm info using node#v15.11.0
...
...
npm verb publish [ '.' ]
npm notice
npm notice 📦 #scope/mypackage#0.1.0
npm notice === Tarball Contents ===
npm notice 214B README.md
npm notice 1.1kB package.json
npm notice === Tarball Details ===
npm notice name: #scope/mypackage
npm notice version: 0.1.0
npm notice filename: #scope/mypackage-0.1.0.tgz
npm notice package size: 764 B
npm notice unpacked size: 1.3 kB
npm notice shasum: c22a42756de43e282da01f33c7d5da4940c7d1d7
npm notice integrity: sha512-l/P2cr52Lle7h[...]isu3rDME3lYuQ==
npm notice total files: 2
npm notice
npm verb stack Error: This command requires you to be logged in.
npm verb stack at Publish.publish (/usr/local/lib/node_modules/npm/lib/publish.js:104:29)
npm verb cwd /home/user/Workspace/mypackage
npm verb Linux 5.8.0-43-generic
npm verb argv "/usr/local/bin/node" "/usr/local/bin/npm" "publish" "--verbose"
npm verb node v15.11.0
npm verb npm v7.11.2
npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in.
npm ERR! need auth You need to authorize this machine using `npm adduser`
npm verb exit 1
npm timing npm Completed in 352ms
npm verb code 1
npm ERR! A complete log of this run can be found in:
npm ERR! /home/user/.npm/_logs/2021-05-12T11_23_19_273Z-debug.log
As you can see, npm publish --verbose isn't being helpful in telling me the URL i'm trying to publish to. Checking the documentation again showed I was missing the trailing slashes after 'packages/npm'.
With the trailing slashes, I was able to publish to the gitlab npm package repository for that project
$ npm config set #scope:registry https://gitlab.company.com/api/v4/projects/123/packages/npm/
$ npm config set //gitlab.company.com/api/v4/projects/123/packages/npm/:_authToken 'MyGeneratedAccessToken'
And for all packages I use
$ npm config set #scope:registry https://gitlab.company.com/api/v4/packages/npm/
$ npm config set //gitlab.company.com/api/v4/packages/npm/:_authToken 'MyGeneratedAccessToken'

I had the same issue for npm publish. I tried with yarn publish and it also failed.
It was successful when I ran:
yarn publish --non-interactive
Also, I had an issue related to .husky (cannot install husky) and it was solved running before yarn publish this command with npm:
npm set-script prepare ''

Related

Can not "npm publish" to internally-hosted npm repository?

I followed Using Nexus 3 as Your Repository – Part 2: npm Packages to set up an internal npm repository.
I'm then able to do this,
$ npm view jsreport dist.tarball
https://registry.npmjs.org/jsreport/-/jsreport-3.0.1.tgz
I have this in my ~/.npmrc file
$ cat ~/.npmrc
//internal_nexus_url:8081/repository/:_authToken=NpmToken.43b77b61-2492-39b8-8150-38555f6b6943
I have this in my /path_to_project/.npmrc file
registry="http://internal_nexus_url:8081/repository/npm-private/_auth=base64_user_pw"
and this in my /path_to_project/package.json file
{
...
"publishConfig": {
"registry": "http://internal_nexus_url:8081/repository/npm-private"
},
...
}
When do an npm publish I get
npm notice === Tarball Details ===
npm notice name: my-package
npm notice version: 2.4.4
npm notice filename: my-package-2.4.4.tgz
npm notice package size: 141.6 MB
npm notice unpacked size: 421.1 MB
npm notice shasum: 09c134ef93ce70e999d62820b5bffc6cf23765f3
npm notice integrity: sha512-X5iqe9DV03MPG[...]TKvNZO4O8ToyQ==
npm notice total files: 17559
npm notice
npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in.
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! /patch/.npm/_logs/2021-11-16T21_48_03_450Z-debug.log
The error file above basically says the same thing. So I follow the instructions, and do
$ npm adduser
$ npm adduser
npm notice Log in on http://internal_nexus_url:8081/repository/exa-npm-private/_auth=base64_user_pw
Username: nexus-user
Password:
Email: (this IS public) nexus#kmha.com
npm ERR! code E401
npm ERR! Unable to authenticate, need: BASIC realm="Sonatype Nexus Repository Manager"
npm ERR! A complete log of this run can be found in:
npm ERR! /path/.npm/_logs/2021-11-16T21_52_29_060Z-debug.log
What's preventing me from publishing to my internal npm repo?
UPDATE:
I modified my ~/.npmrc file to include the information on How should I set _auth in .npmrc when using a Nexus https npm registry proxy? to no avail.

Npm install does not installs secure package though i do npm login followed by npm install commands

Even after logged in using npm login command successfully i get the below error
npm whoami
npm ERR! code E401
npm ERR! 401 Unauthorized - GET https://registry.npmjs.org/-/whoami
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/bharathkumar/.npm/_logs/2021-09-09T08_42_29_365Z-debug.log
Try removing contents/remove file .npmrc in the project folder or the user's home folder

Unable to publish npm package from Github?

I've published react-push-notify by using this command :
npm login
npm publish
But when I want to publish again through Github ( private repository ) by following these steps:
Add publishPackage to my package.json
"publishConfig": { "registry": "https://npm.pkg.github.com/" }
Authenticate
npm login --registry=https://npm.pkg.github.com/
Publish
npm publish
It invokes this error:
npm ERR! code E404
npm ERR! 404 Not Found - PUT https://npm.pkg.github.com/react-push-notify
npm ERR! 404
npm ERR! 404 'react-push-notify#0.2.0' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
This is my first package on npm and I'm confusing what's difference between publishing to npm and publishing to npm from Github?
Login & Logout
npm login --scope=#[Organization name] --registry=https://npm.pkg.github.com
npm logout --scope=#My-Organization --registry=https://npm.pkg.github.com
Publish
npm publish

Not able to setup a local npm repo

I wrote a library and I want to test that people can import it from npm and use it as expected. Towards this I set up a local NPM repo like
npm install -g local-npm
npm set registry http://127.0.0.1:5080
local-npm
This started a local repository on my computer. Now I went to my library project and I did
npm publish
but I get an error
npm ERR! code ENEEDAUTH
npm ERR! need auth auth required for publishing
npm ERR! need auth You need to authorize this machine using `npm adduser`
I did
npm adduser
but I get an error
npm adduser
Username: foo
Password:
Email: (this IS public) foo#bar.com
npm ERR! code EAUTHIP
npm ERR! Unable to authenticate, need: Basic
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/.npm/_logs/2018-04-07T17_46_18_631Z-debug.log
For this local repo. I don't want any users etc because this is only for testing. I want to be able to straight publish to this without any credentials or users. is that possible?
Also, another question is that now that I have setup a local npm and I do a npm i react -D in my project the installation would fail because this repository doesn't contain react. So is it possible that when someone does npm i react -D it still goes to the original NPM repository when when I do npm i foo-lib -D only then it comes to my repository as a fallback because the original npm repository doesn't contain my foo-lib it was only published to this local repository.
Edit: I tried npm publish --registry http://127.0.0.1:5800 and I got the following error
0/bin/npm" "publish" "--registry" "http://127.0.0.1:5080"
23 verbose node v9.5.0
24 verbose npm v5.7.1
25 error code ENEEDAUTH
26 error need auth auth required for publishing
27 error need auth You need to authorize this machine using `npm adduser`
28 verbose exit [ 1, true ]
So it won't let me create a user and it won't let me publish without a user.
Edit2:: I tried the other way round as well.
First run local-npm now when the server starts up
from another window do
npm set registry http://127.0.0.1:5080
npm publish --registry http://127.0.0.1:5080
npm publish
everything results in
~/IdeaProjects/t > npm publish --registry http://127.0.0.1:5080
npm ERR! code ENEEDAUTH
npm ERR! need auth auth required for publishing
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! /Users//.npm/_logs/2018-04-07T18_32_53_815Z-debug.log
~/IdeaProjects/t > npm adduser
Username: foo
Password:
Email: (this IS public) foo#bar.com
npm ERR! code EAUTHIP
npm ERR! Unable to authenticate, need: Basic
npm ERR! A complete log of this run can be found in:
npm ERR! /Users//.npm/_logs/2018-04-07T18_33_25_737Z-debug.log
file contents
19 verbose argv "/Users/a/.nvm/versions/node/v9.5.0/bin/node" "/Users/a/.nvm/versions/node/v9.5.0/bin/npm" "adduser"
20 verbose node v9.5.0
21 verbose npm v5.7.1
22 error code EAUTHIP
23 error Unable to authenticate, need: Basic
Try this:
npm publish --registry http://127.0.0.1:5080
https://docs.npmjs.com/misc/registry#can-i-run-my-own-private-registry

Npm throws 'must have a URL that starts with http: or https:' when installing #angular2-material packages

I'm installing #angular2-material/core ( or any other module ) with npm wont let me install any of the modules.
npm ERR! Darwin 15.5.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "#angular2-material/core"
npm ERR! node v5.11.1
npm ERR! npm v3.8.6
npm ERR! must have a URL that starts with http: or https:
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
I've tried :
npm set registry https://registry.npmjs.org/ // https
OR
npm set registry http://registry.npmjs.org/ // http
But It wont work , I raised the issue with material guys but they're saying this is my local issue .
This is really killing me , there is no clue how to fix it at all.
https://github.com/angular/material2/issues/720
This is mainly because you've tried multiple registries and now npm is kind of confused , You need to open your .npmrc and remove unnecessary registry paths.
To find your .npmrc :
npm config ls -l | grep config
You will see something like :
userconfig /Users/Yourname/.npmrc
Then open that file with your editor and edit and remove extra unnecessary registries .