E401 Unable to authenticate, your authentication token seems to be invalid

avatar
Borislav Hadzhiev

Last updated: Jun 1, 2023
5 min

banner

# Table of Contents

  1. E401 Unable to authenticate, your authentication token seems to be invalid
  2. Remove your package-lock.json file and reinstall your dependencies
  3. Try to set your NPM registry correctly
  4. Solving the error when using Azure
  5. Check if your package.json file contains a supported Node.js version

# E401 Unable to authenticate, your authentication token seems to be invalid

The NPM 401 error "Unable to authenticate, your authentication token seems to be invalid" occurs when you've specified incorrect credentials in your .npmrc file.

To solve the error, make sure the contents of your .npmrc file are correct, delete your package-lock.json and reinstall your dependencies.

Here is the complete error message:

shell
npm ERR! code E401 npm ERR! Unable to authenticate, your authentication token seems to be invalid. npm ERR! To correct this please try logging in again with: npm ERR! npm login

The first thing you should check is if your .npmrc file contains private repo credentials.

  • On macOS and Linux, the file is located at ~/.npmrc.

  • On Windows, the file is located at C:\Users\%username%\.npmrc.

For example, if you are on macOS or Linux, you can open the file in gedit, nano or vim and check if it contains private repo credentials that would prevent you from running npm install.

shell
# with Gedit sudo gedit ~/.npmrc # or with Nano sudo nano ~/.npmrc

You can also try to temporarily rename the ~/.npmrc file and see if the issue is resolved.

On macOS and Linux, run the following command to create a copy of the file.

shell
cp ~/.npmrc ~/.npmrc_copy

And then remove the original file by running the following command.

shell
rm -f ~/.npmrc

If you don't want to remove the file, try removing its contents or most of its contents that you don't need.

If your NPM registry requires authentication, try to rerun the npm login command.

shell
npm login

When you run the npm login command and enter your credentials, your .npmrc file automatically gets updated.

# Remove your package-lock.json file and reinstall your dependencies

If the issue persists, remove your package-lock.json file and reinstall your dependencies.

If you are on Windows, open your terminal in your project's directory (where your package.json file is) and run the following commands.

cmd
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock npm cache clean --force npm install

If you are on macOS or Linux, open bash or zsh in your project's root direct and run the following commands.

shell
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock npm cache clean --force npm install

Your old package-lock.json file might've had a resolved field that tries to access a private repository.

# Try to set your NPM registry correctly

If the issue persists, try to set your npm registry correctly.

Open your terminal and run the following commands.

shell
npm cache clean --force npm config set registry https://registry.npmjs.org

npm config set registry correctly

Check if the error is resolved after running the 2 commands.

You can also try to run the npm install command with the --registry option.

Use the following command.

shell
# ✅ npm install --registry https://registry.npmjs.org

npm install with registry flag

Instead of the following command.

shell
# ⛔️ npm install

# Solving the error when using Azure

If you got the error when using Azure, try to:

  1. Delete or temporarily rename your C:\Users\%username%\.npmrc file.
  2. Open your terminal in the project's directory (where the local .npmrc is located) and run the following command.
shell
npx vsts-npm-auth -config .npmrc

The syntax for the command is:

hell
npx vsts-npm-auth -config path-to-your\.npmrc

You can read more about the vsts-npm-auth package in the package's NPM page.

You can also try to run the npm logout command first.

shell
npm logout

Then run the vsts-npm-auth command.

shell
npx vsts-npm-auth -config .npmrc

If you use Azure, the issue also occurs if your personal access token expires.

You can renew your Personal access token in Azure DevOps > User Settings > Personal Access Tokens.

You can also try to log out and then log in.

shell
npm logout npm login

# Check if your package.json file contains a supported Node.js version

If the issue persists, check if your package.json file contains the Node.js version that you should be using when running npm install.

The supported Node.js versions are usually specified under the engines property in package.json.

package.json
"engines": { "npm": ">=8.0.0 <9.0.0", "node": ">=16.0.0 <17.0.0" }

If your package.json file contains a supported version range, make sure your Node.js and NPM versions match.

You can get your Node.js and npm versions by issuing the following commands.

shell
node -v npm -v

get nodejs and npm versions

Usually, a safe bet is to install the LTS (long-term supported) Node.js version.

  1. One way to download and install the LTS Node.js version is to use the official nodejs.org website.

install lts node version from official site

  1. Alternatively, you can use the nvm package to manage your Node.js version.

If you already have nvm installed, run the following 2 commands to install the LTS version and switch to it.

shell
nvm install --lts nvm use --lts

nvm install and use lts

If you don't have nvm installed, click on the link that relates to your operating system:

After you install and switch to the LTS Node.js version, run the node -v command to verify.

shell
node -v

If you had to switch your Node.js version, you most likely will also have to reinstall your node modules.

If you are on Windows, open your terminal in your project's directory (where your package.json file is) and run the following commands.

cmd
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock npm cache clean --force npm install

If you are on macOS or Linux, open bash or zsh in your project's root direct and run the following commands.

shell
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock npm cache clean --force npm install

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.