15

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.

Apples
  • 3,135
  • 1
  • 21
  • 26
fragile_frogs
  • 371
  • 2
  • 8
  • Does this answer your question? [How to have multiple NPM users set up locally?](https://stackoverflow.com/questions/30114166/how-to-have-multiple-npm-users-set-up-locally) – Anhdevit Oct 19 '20 at 08:50
  • 3
    Unfortunately not, the accepted answer is using multiple projects/folders. I am looking for a solution that works in the same folder/same .npmrc file – fragile_frogs Oct 19 '20 at 08:55
  • Was a workaround ever found for this? – Apples Jul 21 '23 at 16:26

2 Answers2

10

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.

fragile_frogs
  • 371
  • 2
  • 8
6

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.

sfsr
  • 1,091
  • 1
  • 10
  • 9