Unable to publish to an NPM Registry (local)

Nexus

Nexus Problem Overview


I am running Nexus 3.0.1-01, and am using it to host both Maven repositories and NPM registries. For NPM, I have a local mirror of npmjs.org, a local NPM registry and a group that combines the two...

enter image description here

I have been using this with npm internally, where I can use the npm-public group as my registry and this has been working fine. So, I can use Nexus to mirror npmjs.

The next step is to take locally written npm modules and publish them to npm-releases (on my Nexus instance) so that these modules can be shared amongst the delivery teams here. I've been able to build out a package, and npm pack seems to behave.

I have run npm adduser to provide my Nexus credentials to my npm environment. I am using the same username/password I use when I log into the Nexus web app, and my user is assigned the admin role (so I should have all permissions). I can see the credentials in my .npmrc file

enter image description here

My registry value is still the npm-public group which combined the mirror and my local registry. I have ensured that the package.json of the module I am attempting to deploy has a "publishConfig" section that points to the url of the local registry (not the public group)

enter image description here

However, despite all of that, calling "npm publish" results in a 401 error...

enter image description here

Looking at the npm-debug.log, I can see it's attempting to call the HTTP PUT call to push the assembled tgz file to the registry, and this is returning a 401 error

enter image description here

I have enabled debug logging on the shiro package in the server, but I only ever see a single message thinking it needs authentication

2016-09-13 08:56:28,590+1000 DEBUG [qtp1257823896-4030] *UNKNOWN org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter - Authentication required: sending 401 Authentication challenge response.

According to the documentation, I've done all I'm supposed to do (npm add user) but I am unable to successfully deploy an npm module to my local repository.

Are there additional options I need to use when calling npm publish? Are there additional settings I need to make to the hosted npm repository in my server that will allow me to publish to it? I pretty much created it using the default values. Have I missed a step that is preventing me from deploying to my hosted npm registry?

Any help would be greatly appreciated, as I am stuck at the moment.

Nexus Solutions


Solution 1 - Nexus

As it turns out, I did not have the Npm Bearer Token Realm in my list of active realms. Once I moved it to Active, the publish completed successfully!

enter image description here

Solution 2 - Nexus

Additonally to @EdH's answer we discovered that the format of .npmrc has changed so base64 encoded _auth will not work anymore... and the token has to be created by logging into the repo.

old .npmrc

registry=https://host/repo
_auth=12afdjsljl123213

new .npmrc

//host/repo/:_authtoken=uuidOfToken

Solution 3 - Nexus

Additionally to @Daniel's answer, I discovered that when adding user credentials/logging into npm, you can't have a trailing slash on the registry url.

Bad:

npm adduser --registry=https://repo.localhost/repository/npm-internal/

Good:

npm adduser --registry=https://repo/repository/npm-internal

Also, if you're looking to automate the login (i.e. non-interactively), I used a good tool called npm-cli-login.

npm-cli-login -u admin -p admin123 -e nick@foo.bar -r https://repo/repository/npm-internal

Solution 4 - Nexus

I realize that this post is a couple years old now, but as of struggling with this issue for many hours, I finally found what worked for me that I've not seen in many other places. The problem was that I was not specifying the _authToken within the .npmrc file. Adding this as shown below resolved the issue and I was able to successfully run npm commands without a 401 error.

.npmrc

registry=https://test.repo.com/repository/npm-group/
//test.repo.com/repository/npm-group/:_authToken=NpmToken.${NPM_TOKEN}

Solution 5 - Nexus

Another reason why you might get such an error is that you might have enabled anonymous access to the server for the 'npm Bearer Token Realm'. After disabling anonymous access you must also remove .npmrc and re-add the registry and adduser.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEdHView Question on Stackoverflow
Solution 1 - NexusEdHView Answer on Stackoverflow
Solution 2 - NexusDaniel SchröterView Answer on Stackoverflow
Solution 3 - NexusNick GrealyView Answer on Stackoverflow
Solution 4 - NexustripleplayjView Answer on Stackoverflow
Solution 5 - NexusravenView Answer on Stackoverflow