114

I'm trying to connect to a Azure DevOps Artifacts feed, but I keep getting an E401 error.

I've tried on a different computer and it connected just fine. I've uninstalled, and reinstalled my Node.js, and I still got the error.

Below is the error I get when I run 'npm install'

npm ERR! code E401
npm ERR! Unable to authenticate, need: Bearer authorization_uri=https://login.windows.net/*********, Basic realm="https://pkgsprodcus1.pkgs.visualstudio.com/", TFS-Federated

Below is what was added to the .npmrc file

registry=https://[org].pkgs.visualstudio.com/_packaging/[feed].Npm/npm/registry
//pkgs.dev.azure.com/[org]/_packaging/[feed].Npm/npm/registry/:_authToken=[token]
//pkgs.dev.azure.com/[org]/_packaging/[feed].Npm/npm/:_authToken=[token]

I expected to be connected to the feed after adding my info to the .npmrc file, and running 'npm install'

0

14 Answers 14

233

I had an issue where I couldn't connect, even though I had the same .npmrc as other repos on the same machine. Running vsts-npm-auth -config .npmrc just exited, presumably happy with the cached credentials.

However, the credentials it had seemed to be bad. The solution was to force refreshing the token: vsts-npm-auth -config .npmrc -force

Sign up to request clarification or add additional context in comments.

12 Comments

Where to run this? I tried in Powershell and regular command prompt, also in Node command prompt, every where it is saying it is not recognized command.
npm install -g vsts-npm-auth and if the command doesn't work after opening a new command line, npx vsts-npm-auth
This issue happens often, whenever the token expired. This force refresh command works just fine... thanks for this. You save my day brother.
Thanks. I'm having a weird problem where my VM can't use SSL without a certificate error. Using -force allowed the window to stay open so I could click 'visit web site anyway'.
vsts-npm-auth -config .npmrc -force did the work for me, thanks a lot.
|
32

This usually happens when you've recently changed your password. To fix this problem, I ran this command within VS Code's Powershell terminal (any terminal will work)

vsts-npm-auth -config .npmrc

With doing so, a GUI popped-up where I was able to select the account I wanted to use to authenticate.

3 Comments

If this doesn't work at first try installing the package npm install -g vsts-npm-auth and then above command.
vsts-npm-auth it will work only on Windows machine so its not a solution
vsts-npm-auth is not available for macOS
17

When Microsoft updated changed the name of VSTS to Azure DevOps, they also changed many of the URLs. Most of the old ones still redirect, but it looks like you have a mismatch in your .npmrc file above. Change the URI in the first line to match the other lines, so it looks like this:

registry=https://pkgs.dev.azure.com/[org]/_packaging/[feed].Npm/npm/registry

That should unblock you.

As a note, if you have both the registry and your credentials in the same file, and that file is being committed to source control, you are also saving your credentials to source control. Best practice here is to keep .npmrc in your project root that only has that first line, the one that tells npm which registry to connect to. Then, create a new file at ~/.npmrc (if you are using Windows you can use git bash to help put it in the right place) and just add the other two lines, the ones with your credentials. This will keep your creds local to your machine and the file with the registry note can safely be committed.

Also, remember that the credentials you generate from the Azure package registry are good for 90 days at the most, so at some point you will get the unauthorized error message again and you will need to update the credentials in your local ~/.npmrc file.

1 Comment

Great catch, I also had a mismatch in my .npmrc URIs and go the same issue; Making sure the URIs are the same solved the issue for me (Node v20)
14

I just experienced this gotcha with the Azure DevOps portal:

One of my colleagues couldn't connect. It turned out that the Azure DevOps portal inserted the text %40Local after the feed name if you copy the feed URL in the browser as I did when sharing it over MS Teams.

What my portal showed:

; begin auth token
//pkgs.dev.azure.com/<company>/_packaging/<feed>/npm/registry/:username=xxx
//pkgs.dev.azure.com/<company>/_packaging/<feed>/npm/registry/:_password=[BASE64_ENCODED_PERSONAL_ACCESS_TOKEN]
//pkgs.dev.azure.com/<company>/_packaging/<feed>/npm/registry/:email=npm requires email to be set but doesn't use the value
//pkgs.dev.azure.com/<company>/_packaging/<feed>/npm/:username=xxx
//pkgs.dev.azure.com/<company>/_packaging/<feed>/npm/:_password=[BASE64_ENCODED_PERSONAL_ACCESS_TOKEN]
//pkgs.dev.azure.com/<company>/_packaging/<feed>/npm/:email=npm requires email to be set but doesn't use the value
; end auth token

What his portal showed:

; begin auth token
//pkgs.dev.azure.com/<company>/_packaging/<feed>%40Local/npm/registry/:username=xxx
//pkgs.dev.azure.com/<company>/_packaging/<feed>%40Local/npm/registry/:_password=[BASE64_ENCODED_PERSONAL_ACCESS_TOKEN]
//pkgs.dev.azure.com/<company>/_packaging/<feed>%40Local/npm/registry/:email=npm requires email to be set but doesn't use the value
//pkgs.dev.azure.com/<company>/_packaging/<feed>%40Local/npm/:username=xxx
//pkgs.dev.azure.com/<company>/_packaging/<feed>%40Local/npm/:_password=[BASE64_ENCODED_PERSONAL_ACCESS_TOKEN]
//pkgs.dev.azure.com/<company>/_packaging/<feed>%40Local/npm/:email=npm requires email to be set but doesn't use the value
; end auth token

That caused a mismatch with the repository URL in the projects .npmrc

To fix it: Make sure the URL is correct and is matching in both user .npmrc and project .npmrc

Comments

5

On my side I needed to delete the file in my user folder folder first.

rm C:\Users\<YourUserName>\.npmrc

After that I was able to run

vsts-npm-auth -config .npmrc

1 Comment

This one saved me
3

None of the many answers I've seen worked for me. Here's what did work for me:

If you haven't already then run: npm i -g vsts-npm-auth
Open Powershell and run: vsts-npm-auth -config .npmrc -T $HOME/.npmrc

Those 2 commands fixed my case.

3 Comments

How do you run npm i -g vsts-npm-auth if you have this issue?
@runnerpaul That command would only install the vsts-npm-auth package globally on your system, which is independent of this issue?
@Kim Skogsmo I think runnerpaul is saying that when you try to download the vsts-npm-auth that one will also give the 401 as the authorization failed. So you will never get passed that, in this case. (PAT token should help to make the first download though)
2

I've had the same error message for a slightly different reason and was scratching my head for a long time over it. Just posting here in case it helps anyone else.

vsts-npm-auth for me is outputting the new .npmrc file with its token/credentials into a .npmrc file in an old 'homes' share mapped to W: drive. (still don't know why it's doing that)

whereas npm was looking for those credentials/tokens in c:\users\[username]\.npmrc and finding old stale creds.

my workaround was to copy and paste the creds from w:\.npmrc and paste them over the creds in c:\users\[username]\.npmrc

Comments

1

My issue was caused by the fact that running vsts-npm-auth command wrote the URLs for username and password into my .npmrc file at %USERPROFILE%\.npmrc that were missing the last URL segment /registry. I added the /registry segment to my user .npmrc file and all is well again.

Comments

1

First, make sure that you have installed 'vsts-npm-auth' package. If not run:

npm install -g vsts-npm-auth

Then run vsts-npm-auth -config .npmrc

It will ask you to authenticate/verify your azure devops account.

Then run npm install

Comments

1

I had the same problem authenticating with an Azure DevOps hosted npm feed when trying to run npm install from WSL when I checked my project out to the /home/[username]/src/[projectName] directory.

I tried running vsts-npm-auth -F -config .npmrc from the same directory multiple times and a new token would get generated in Azure DevOps but I would still get the same error when running npm install.

The odd thing was when I checked out the code under WSL to /mnt/c/src/[projectName] authentication would work when running npm install.

I was able to resolve this by manually copying the relevant lines from the .npmrc file in windows at c:\Users\[UserName]\.npmrc to the .npmrc file in WSL at /root/.npmrc.

After I did that, authentication worked when running npm install.

Comments

0

We also encountered this issue when multiple scoped artifact feeds were in use but none of the answers here helped.

Running vsts-npm-auth -config .npmrc worked successfully, but when running npm commands, it would respond with the error mentioned in the OP's question.

We added the following line into our project's .npmrc file:

always-auth=true

After doing this, we re-ran the npm command and successfully authenticated.

Comments

0

follow the steps

  1. npm install -g vsts-npm-auth
  2. vsts-npm-auth -config ./$filepath/.npmrc you should mention the file path

if the issue still exists then use(it will redirect to a popup there you want to re login with your credentials) 3. vsts-npm-auth -config ./$filepath/.npmrc -r -f -v normal

Comments

0

Go to feeds in artifacts section then click on 'connect to feed' and select npm then you will be able to see this line: "Having issues? See the instructions for using a Personal Access Token to authenticate."

Click on this line then some content(token) will come up, replace your old content of user level .npmrc file with this content and save it. Close and open your editor again. It has worked for me and hopefully it will also work for you.

Comments

0

please try this way

  1. Clear your temp files(%temp%)
  2. Please check your .npmrc file containing the "always-auth=true"
  3. In VS Code, Open terminal and Navigating to your .npmrc file location
  4. Execute the command "npx vsts-npm-auth -config .npmrc" then you can receive the below success message

vsts-npm-auth v0.43.0.0

Getting new credentials for source:https:xxxxx.xxx.xxx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.