Perhaps you are working within an environment, which requires specific npm registry available behind corporate VPN or internal network. Maybe, you like to customize your npm profile settings, but your configuration should not be the default one.
How to improve your workflow with multiple npm configurations?
Project specific file
You can set config file per project you are working on, by adding .npmrc file to your project root directory e.g. /path/to/my/project/.npmrc.
# Set a new registry for a scoped package
registry=https://registry.example.com
package-lock=true
yes=true
legacy-peer-deps=true
This approach is very handy, when working with a team, as simple npm install will work right away without any action required from your team members.
Managing profiles with npmrc
Another option is managing configuration files with npmrc, which can be installed globally with:
npm i npmrc -g
To make sure installation went fine, list all available profiles using:
npmrc
Then you can add a new profile named work with:
npmrc -c work
And select it with:
npmrc work
Finally, apply address of the registry you are trying to reach:
npm config set registry https://registry.example.com
It is also possible to apply separate user settings per profile, by using:
npm adduser
and providing username, password and email.
A useful tip for CI script usage is to read authToken from .npmrc file for desired profile and using it as a secret within your version control service like GitLab.
This approach allows switching between different npm profiles with ease. To switch back to your default profile, you can use:
npmrc default
You can set other configuration options for each profile specified here.
Last updated: December 8, 2022