35

I want to globally setup an NPM registry for a specific scope to be used with a specific token.

I know that I can use :

$ npm login --scope=@organisation

And I can also write a ~/.npmrc with :

//registry.npmjs.org/:_authToken=XXXX

But what I want is a combinaison of the two methods: Using the token at when assigning the registry URL to my scope.

I tried :

npm config set @organisation:registry https://registry.npmjs.org/:_authToken=XXXX

But when running an NPM command (eg npm install @organisation/my-package). I get the following error :

npm ERR! Darwin 15.6.0
npm ERR! argv "/Users/me/.nvm/versions/node/v6.2.2/bin/node" "/Users/me/.nvm/versions/node/v6.2.2/bin/npm" "install" "@organisation/my-package"
npm ERR! node v6.2.2
npm ERR! npm  v3.10.3
npm ERR! code E403
npm ERR! you do not have permission to publish ":_authToken=XXXX". Are you logged in as the correct user? : :_authToken=XXXX

Is there a solution? (I need to use a token and no env variable).

PS: https://github.com/npm/npm/issues/7995#issuecomment-175915766 but it's not working...

Yves M.
  • 29,855
  • 23
  • 108
  • 144

1 Answers1

77

According to the official documentation you should be able to associate a scope with a registry when logging in. Is this what you want?

Did you try the following?

npm login --registry=https://reg.example.com --scope=@myco

If you dont want to login, but rather want to specify the token explicitly, the following should work:

npm config set @myco:registry https://reg.example.com
npm config set //reg.example.com/:_authToken xxxxxxxx

Note that the registry url must be normalized for this to work, ie it shouldn't include scheme and must end with a slash.

Drazen Bjelovuk
  • 5,201
  • 5
  • 37
  • 64
deadbeef
  • 1,151
  • 10
  • 7
  • Having problems doing npm install through token. are the quotes '' supposed to be there, and the sample registry url above looks like it doesn't end with a slash – Shaun Chua Aug 16 '18 at 21:22
  • Just to add: 'npm config set' will just put these two line put this into ~/.npmrc – Frank Sep 03 '19 at 11:45
  • What if npm is your registry? You just want a token only for a specific scope? – Tiago Jul 04 '23 at 11:05