I am using two NPM accounts: a public one and a private one. How would I set it up so that I don\'t need to npm login
every time I publish a module on a different a
This is how I'm solving it having 4 different NPM logins.
.gitignore
(and .npmignore
for NPM modules) add this line: .npmrc
. This will make sure you never commit (or publish) the .npmrc
file..npmrc
file containing this: //registry.npmjs.org/:_authToken=11111111-1111-1111-1111-111111111111
(replace the GUID with an actual NPM auth token, e.g. you can grab it from ~/.npmrc
)The npm
CLI will look in your current folder for the .npmrc
file (or in any parent folder) and will use it for auth.
As the result all npm
commands work as is, no need to pass --userconfig
or anything.
In addition to the above you can have the default NPM token across your computer/laptop.
.npmrc
is NOT present in .gitignore
(which is common for most projects out there)..npmrc
file in the root folder of your project. Put this inside: //registry.npmjs.org/:_authToken=${NPM_TOKEN}
. This will make npm
to use NPM_TOKEN
env. var. And npm
will abort if such env. var. is not found.NPM_TOKEN
environment variable set. E.g. NPM_TOKEN=11111111-1111-1111-1111-111111111111
. I have it in my ~/.bash_profile
.All the projects, which have this file committed, will use your environment variable NPM_TOKEN
for npm auth.
As the result all npm commands work as is, no need to pass --userconfig
or anything.
This is good and secure enough for CI (Continuous Integration). All CI-s allow you to set environment variables. Using this approach you can change the NPM user with a simple env. var. change.
Pro Tip
Type npm whoami
command to check which token is currently being used in the folder.