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.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay