How to npm login as a different user per scope on same registry?

4.5k Views Asked by At

I need to set up multiple npm authTokens that are for two different scopes, but both scopes are on the same registry.

@scope1:registry=https://registry.npmjs.org/
@scope2:registry=https://registry.npmjs.org/
//registry.npmjs.org/:_authToken=${NPM_TOKEN}

I have tried to add the an authToken to each scope like this:

@scope1:registry=https://registry.npmjs.org/:_authToken=${TOKEN_1}
@scope2:registry=https://registry.npmjs.org/:_authToken=${TOKEN_2}

But I get a 405 error when I try to install a package.

I tried reading docs, but I cannot find a way to specify different tokens for different scopes on the same registry.

2

There are 2 best solutions below

0
On

I contacted the npm support and got the following response:

Users cannot mix-and-match auth for a single registry. We recommend having a single npm user that's granted access to all of the packages and to use the token for that user.

3
On

This is a bit of a hacky answer - but it works. At least on github packages where I am using it. I assume it works on npmjs.org but I don't have any private packages there to test.

@scope1:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=TOKEN1

@scope2:registry=https://npm.pkg.github.com:443
//npm.pkg.github.com:443/:_authToken=TOKEN2

@scope3:registry=https://npm.pkg.github.com:0443
//npm.pkg.github.com:0443/:_authToken=TOKEN3

Basically, just make them different registries by specifying a port even though it is the same port.