45

I am having authentication problem when publishing to my private npm registry hosted on my private Nexus.

My Nexus setup is I have npm-proxy, npm-registry (hosted npm with allowRepublish=false), npm-snapshots (hosted npm with allowRepublish=true) and npm-public (group with all other three repositories).

Since I am developing a library, I am using my snapshot repository, so I can redeploy same version constantly (something like snapshot in maven world).

In my library project I have set this option in package.json

"publishConfig": {
    "registry": "https://my.nexus.com/repository/npm-snapshots/"
}

Next, I created .npmrc file with following content:

registry=https://my.nexus.com/repository/npm-public/
_auth=RVhBTVBMRQ==

And with this setup I can publish project with no problem. However, what bothers me, is that I have my password (which is just base64 encoded) stored in file, that should be commited, but I can't commit it, due to credentials in it.

I have tried to instead login to npm registry and removed the auth line from .npmrc npm adduser --registry=https://my.nexus.com/repository/npm-snapshots --always-auth

I got response Logged in as myusername on https://my.nexus.com/repository/npm-snapshots.

However, when I try to run npm publish I get:

npm ERR! code E401
npm ERR! Unable to authenticate, need: BASIC realm="Sonatype Nexus Repository Manager"
npm verb exit [ 1, true ]
npm timing npm Completed in 6867ms

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\XXXX\AppData\Roaming\npm-cache\_logs\2019-07-30T19_31_01_598Z-debug.log

Now in my other project (which is using this library), I simply created .npmrc file with content registry=https://nexus.mjamsek.com/repository/npm-public/ and run command npm adduser --registry=https://my.nexus.com/repository/npm-public --always-auth and I was able to download the published package.

However, the publish still won't work and I don't know why.

EDIT 31.7.2019: On my list of active realms I also have npm Bearer Token Realm

2
  • 4
    You need a trailing slash on the end of the registry URL passed into "npm adduser", otherwise npm will chop off the last segment of the URL, and it won't work.
    – rseddon
    Jul 31, 2019 at 14:19
  • Indeed that was the problem, thank you. If you write it as an answer, I will accept it. Jul 31, 2019 at 17:50

11 Answers 11

54

When you do npm login or npm adduser the NPM client creates an authentication token that will be used in future request to the registry. Default NXRM configuration allows only Local Authenticating Realm which doesn't recognise NPM's token. Please make sure you have npm Bearer Token Realm active.

enter image description here

2
  • 1
    Hi, I already have this on list of active realms, I updated the question. Jul 31, 2019 at 7:36
  • image is not available Aug 10 at 12:45
34

You need a trailing slash on the end of the registry URL passed into npm adduser, otherwise npm will chop off the last segment of the URL, and it won't work.

1
  • 2
    Answer of the YEAR! +100000000000000000000000
    – JAN
    Mar 8, 2021 at 15:16
7

_auth= replaced with output of btoa('username:userpassword') and it worked for me.

I did use this btoa from chrome as below.

enter image description here

0
7

I encountered this problem today, my solution was to delete all registry entry from my npmrc file:

registry=https://my.nexus.com/repository/npm-snapshots/

Idealy delete anything superfluous, back it up before-hand, in my case my file contained only:

strict-ssl=false

Then you can npm login --registry=https://my.nexus.com/repository/npm-public/ again.

If that's not working, you also bypass npm login with curl, look at this life saving post.

1
  • This is important if you work with multiple repositories.
    – PeterS
    Jul 13 at 11:51
4

Make sure the _auth token is correct. In my case I changed my system credentials and forgot to generate new _auth token. I was getting the exact same error i.e. "npm ERR! code E401 npm ERR! Unable to authenticate, need: BASIC realm="Sonatype Nexus Repository Manager"

once i fixed it, the issue was resolved. For those who are looking for the command to generate _auth. It is: btoa('username:userpassword')

2

I had same problem, my solution was to delete my global .npmrc file, and after login npm login.

1
  • This was the one that did it for me thanks. Jan 10 at 16:38
1

I had ended with three versions of node on my machine. It turned out that the ones i installed later had their own local .npmrc files in the node_modules folders. They didn't use the global .npmrc even after i removed the local one so i had to copy it.

1

Usually, you have a repository setup in Nexus which contains:

  • Hosted (repo for your private packages)
  • Proxy (proxy to public packages, e.g. https://registry.npmjs.org)
  • Group (group for the other 2 repos. Nexus will automatically forward requests to public packages to the proxy, and private packages to the hosted)

Like this: Nexus Setup

In my usecase I needed to include the auth-configuration twice for hosted and group to make it work.

registry=https://my-private-nexus/repository/npm-group/
[email protected]
always-auth=true
//my-private-nexus/repository/npm-group/:_auth=<auth-token>
//my-private-nexus/repository/npm-hosted/:_auth=<auth-token>

Successfully tested with node v18.16.0, npm 9.5.1, Nexus OSS 3.53.1-02

0

I was struggling about this problem last two days, finally the solution was to delete .npmrc file from root (user) directory. When npm tried to login, it used the creds inside this file and ignore your pass login.

-1

I've had a similar issue. I also have our credentials stored in an npmrc file in my user directory. When set up with node16/npm7, I would receive the error

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`

If I use nvm to downgrade to node12/npm6, it works. I'd prefer a working solution without downgrading, but for now it lets me move on.

UPDATE:

We finally figured it out (a while ago, but I forgot about this answer). In our .npmrc files in our user directories, we needed to add/change our authorization config entry.

Before:
_auth={base64 encoded username:password}

After:
//{path to private repository}:_auth={base64 encoded username:password}

1
  • any updates? :/
    – r g
    Dec 13, 2021 at 12:41
-3

Just enable anonymous access in the nexus dashboard, it will pull from your private registry.

1
  • 3
    This defeats the whole purpose of private registry Aug 27, 2021 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.