E401 - Unable to authenticate, your authentication token seems to be invalid
Asked Answered
R

12

43

I tested with Azure Packages private NPM server and now want to revert back to using the standard NPM registry but when I do it complains. I have tried everything I can think of and it is blocking me from doing any work now. I'd really appreciate any help.

The error

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

If I check the log it is still, somehow, trying to find packages from Azure rather than the npm registry.

The Azure URL specified below doesnt exist in any .npmrc file or package-lock file I can find!

To be clear here I want to use the default NPM registry not Azure. e.g.

32 silly fetch manifest @types/angular@https://pkgs.dev.azure.com/***/***/_packaging/***.Common.UI/npm/registry/@types/angular/-/angular-1.6.45.tgz

Steps I have taken

In each case, running npm install still gives me the same error.

Please help!

Ratliff answered 6/5, 2021 at 13:57 Comment(1)
I deleted the file package.lock.json and it back to work againGimmick
D
50

.npmrc containing private repo credentials

  • I had similar error. It turned out that I've saved some credentials for private repo on .npmrc file at the root of my home folder.
  • So when I did npm install on my project, I get package-lock.json file contents appended with the private repo url. So this was the source of the error when deploying the project.
  • What I did was to temporarily remove the .npmrc, delete package-lock.json, delete node_modules and re-run npm install.
  • In my case the private repo details was not relevant for the project(so deleting .npmrc was not an issue)
Deceleron answered 16/12, 2021 at 14:44 Comment(4)
This was exactly my issue, deleting package-lock.json and reinstalling node_modules has fixed it, thanks!Premolar
Remove .npmrc from home folder on macAircraft
i did all of this except removing .npmrc and the error went away.Gynecic
Nice! Removing just the package-lock.json solving the code E401 problem for me.Crux
P
18

In my case , I just deleted the package-lock.json file and tried running npm install.

The error disappeared and all packages in node-modules were created. This happened because in the previous package-lock.json file the resolved field had an address that was not for public access.

But my new package-lock.json resolved field looks like this:

"node_modules/@hapi/hoek": {
      "version": "9.3.0",
      "resolved": "https://registry.npmjs.org/@hapi/hoek/-/hoek-9.3.0.tgz",
      "integrity": "sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ=="
    }.
Publicist answered 2/10, 2022 at 6:41 Comment(1)
I didn't even do npm login.Publicist
T
17

Tried all the methods but Nothing worked for me. This steps solved my issue.

  1. Delete the .npmrc file in your Users folder.

    C:\Users\[your user name]

  2. Run this command in your project folder that has an .npmrc file in it:

     npx vsts-npm-auth -config .npmrc
    
Tough answered 10/10, 2022 at 11:28 Comment(1)
just running the second one worked for me.Abseil
X
6

Check your package.json for the node version you should be using and make sure that you are using a compatible version with nvm or something. This has been consistently the reason I have seen this error lately on my own machine.

Xenocryst answered 25/8, 2021 at 15:50 Comment(1)
This is exactly my problem and I was able to fix it by upgrading my node version. Thanks for the tip!Dorice
D
6

if the issue exist entire system check you global .npmrc file for mac its vim ~/.npmrc

set config npm config set registry "https://registry.npmjs.com/"

it will fix issue if problem with npmrc registry

if it happened for a single project it due to the propject .npmrc file.

Dirge answered 23/2, 2023 at 18:1 Comment(0)
C
6

If you have multiple npmrc configurations and you've change one of them.

Change your npmrc to default

You can do this configuration for each one.

npm config set registry "https://registry.npmjs.com/"
Column answered 7/3, 2023 at 7:36 Comment(2)
This answer deserved more than 1 up vote. I had tried: 1) deleting packages-lock.json 2) updating personal .npmrc with latest PAT, 3) making sure my node is up to date, but nothing worked until I tried your comment by running npm config set registrty. Thank you so much for your post.Ratoon
Thanks Stevey I'm glad that this has helped you!Column
V
3

The credentials in the .npmrc file have an expiration time. You need to regenerate these credentials.

Vassalize answered 25/8, 2022 at 14:35 Comment(1)
The solution for me was to change the token located in .npmrcNewbold
P
3

Above @kotana Sie worked for me. But there is no explanation so I would like to add that.

the errors mean that your access key to the private Azure DevOps npm repository has expired and npm can’t login to the repository using it.

To refresh the keys just run to acquire new:

vsts-npm-auth -config .npmrc

There is a known issue with sometimes that doesn't work and just says the keys “are already up to date” or “can’t get an authentication token…”:. To solve it delete the C:\Users\<YourAccountName>\.npmrc manually and repeat the process.

Pilliwinks answered 7/2, 2023 at 5:25 Comment(1)
I'd like to see this upvoted more. If you are using Azure DevOps private NPM repo, NPM falsely reports that you need to login to npmjs.com, when in actuality, you need to refresh your Azure DevOps token.Emileeemili
S
3

I was getting this error for an individual repository. I got it when I ran npm i. The fix is:

  1. Delete .npmrc for this repo
  2. Delete package lock
  3. Run npm i again
  4. add back the deleted .npmrc
Seidler answered 22/3, 2023 at 20:41 Comment(0)
O
0

Had similar issue, Deleting the .npmrc and then doing npm login again solved my issue, it was located in the project directory

Onshore answered 27/1, 2022 at 14:20 Comment(0)
P
0

None of the above answers fixed it for me. I had an additional problem I didn't notice at first and solved it this way:

The project I was working on apparently used an old version of npm and node. So I downgraded both globally via npm install -g [email protected] and npm install -g [email protected] (of course check which versions you need :)).

After that I ran npm install and it worked.

Parahydrogen answered 3/11, 2023 at 9:5 Comment(0)
C
0

For me it was the credentials (personal access token) stored in .npmrc has been expired. You have to generate a new personal access token in your repo. Eg: If you are using gitlab

It's under https://gitlab.com/-/user_settings/personal_access_tokens

Find the .npmrc file and replace the below line with your new access token.

gitlab.com/api/v4/packages/npm/:_authToken=YOUR_NEW_TOKEN

Confusion answered 11/6 at 4:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.