# Table of Contents
- E401 Unable to authenticate, your authentication token seems to be invalid
- Remove your package-lock.json file and reinstall your dependencies
- Try to set your NPM registry correctly
- Solving the error when using Azure
- 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 tobe 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
Copied!
npm ERR! code E401npm 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 repocredentials.
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 wouldprevent you from running npm install
.
shell
Copied!
# with Geditsudo gedit ~/.npmrc# or with Nanosudo nano ~/.npmrc
You can also try to temporarily rename the ~/.npmrc
file and see if the issueis resolved.
On macOS and Linux, run the following command to create a copy of the file.
shell
Copied!
cp ~/.npmrc ~/.npmrc_copy
And then remove the original file by running the following command.
shell
Copied!
rm -f ~/.npmrc
If you don't want to remove the file, try removing its contents or most of itscontents that you don't need.
If your NPM registry requires authentication, try to rerun the npm login
command.
shell
Copied!
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 yourdependencies.
If you are on Windows, open your terminal in your project's directory (whereyour package.json
file is) and run the following commands.
cmd
Copied!
# for Windowsrd /s /q "node_modules"del package-lock.jsondel -f yarn.locknpm cache clean --forcenpm install
If you are on macOS or Linux, open bash
or zsh
in your project's root directand run the following commands.
shell
Copied!
# for macOS and Linuxrm -rf node_modulesrm -f package-lock.jsonrm -f yarn.locknpm cache clean --forcenpm install
Your old package-lock.json
file might've had a resolved
field that tries toaccess 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
Copied!
npm cache clean --forcenpm config set registry https://registry.npmjs.org
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
Copied!
# ✅npm install --registry https://registry.npmjs.org
Instead of the following command.
shell
Copied!
# ⛔️npm install
# Solving the error when using Azure
If you got the error when using Azure, try to:
- Delete or temporarily rename your
C:\Users\%username%\.npmrc
file. - Open your terminal in the project's directory (where the local
.npmrc
islocated) and run the following command.
shell
Copied!
npx vsts-npm-auth -config .npmrc
The syntax for the command is:
hell
Copied!
npx vsts-npm-auth -config path-to-your\.npmrc
You can read more about the vsts-npm-auth
package in thepackage's NPM page.
You can also try to run the npm logout
command first.
shell
Copied!
npm logout
Then run the vsts-npm-auth
command.
shell
Copied!
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
Copied!
npm logoutnpm 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.jsversion 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
Copied!
"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 yourNode.js and NPM versions match.
You can get your Node.js and npm versions by issuing the following commands.
shell
Copied!
node -vnpm -v
Usually, a safe bet is to install the LTS (long-term supported) Node.js version.
- One way to download and install the LTS Node.js version is to use theofficial nodejs.org website.
- 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 theLTS version and switch to it.
shell
Copied!
nvm install --ltsnvm use --lts
If you don't have nvm
installed, click on the link that relates to youroperating system:
- Install NVM on macOS and Linux
- Install NVM on Windows
After you install and switch to the LTS Node.js version, run the node -v
command to verify.
shell
Copied!
node -v
If you had to switch your Node.js version, you most likely will also have toreinstall your node modules.
If you are on Windows, open your terminal in your project's directory (whereyour package.json
file is) and run the following commands.
cmd
Copied!
# for Windowsrd /s /q "node_modules"del package-lock.jsondel -f yarn.locknpm cache clean --forcenpm install
If you are on macOS or Linux, open bash
or zsh
in your project's root directand run the following commands.
shell
Copied!
# for macOS and Linuxrm -rf node_modulesrm -f package-lock.jsonrm -f yarn.locknpm cache clean --forcenpm install
# Additional Resources
You can learn more about the related topics by checking out the followingtutorials:
- How to reset your NPM configuration to the default values
- The module was compiled against a different Node.js version using NODE_MODULE_VERSION
- Failed to load next.config.js Error: Not supported [Solved]
- gulp-sass 5 does not have a default Sass compiler [Solved]
- Error: getaddrinfo ENOTFOUND when making HTTP request [Fix]