Last updated: Jun 1, 2023
Reading time·5 min
package-lock.json
file and reinstall your dependenciesThe 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:
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
.
# 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.
cp ~/.npmrc ~/.npmrc_copy
And then remove the original file by running the following command.
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.
npm login
When you run the npm login
command and enter your credentials, your .npmrc
file automatically gets updated.
package-lock.json
file and reinstall your dependenciesIf 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.
# 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.
# 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.
If the issue persists, try to set your npm registry correctly.
Open your terminal and run the following commands.
npm cache clean --force npm 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.
# ✅ npm install --registry https://registry.npmjs.org
Instead of the following command.
# ⛔️ npm install
If you got the error when using Azure, try to:
C:\Users\%username%\.npmrc
file..npmrc
is
located) and run the following command.npx vsts-npm-auth -config .npmrc
The syntax for the command is:
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.
npm logout
Then run the vsts-npm-auth
command.
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.
npm logout npm login
package.json
file contains a supported Node.js versionIf 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
.
"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.
node -v npm -v
Usually, a safe bet is to install the LTS (long-term supported) Node.js version.
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.
nvm install --lts nvm 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.
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.
# 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.
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock npm cache clean --force npm install
You can learn more about the related topics by checking out the following tutorials: