DEV Community

Cover image for How to automate NPM authentication to avoid providing credentials every time
Deepak Patil
Deepak Patil

Posted on

How to automate NPM authentication to avoid providing credentials every time

You must have used npm for publishing your javascript packages or using private libraries and packages. For developers using npm is a daily habit for building projects. However, It becomes tedious when authenticating NPM for using or publishing any package.

We can avoid this by automating npm authentication. To do so follow the steps below.

Approach 1:

So the naive approach is setting up credentials as environment variables. You can add the following environment variables.

export NPM_CONFIG_EMAIL=<EMAIL>

export NPM_CONFIG__AUTH=<AUTHENTICATION_TOKEN>

export NPM_CONFIG_USERNAME=<USERNAME>

export NPM_CONFIG_PASSWORD=<PASSWORD>

You can get the Auth token from the NPMJS account settings page under Access tokens.

Image description

You can create two types of tokens Granular access token or classic token.

Under classic token, you can create use read-only, automation or publish token. Among these tokens, automation tokens bypass 2Factor Authentication.

While Granular Access tokens provide more security by configuring multiple options such as Validity, IP addresses and permissions.


Approach 2

Another way to perform this is by just updating the .npmrc file in the user's directory. We need to add the following line to the file. Add your auth token in place of .

//registry.npmjs.org/:_authToken=<YOUR_AUTH_TOKEN>
Enter fullscreen mode Exit fullscreen mode

The next time you perform any operations related to NPM you can focus on development as there is no more need to provide credentials every time.

Top comments (0)