How to Fix "Unable to Authenticate" Errors with Azure Artifacts in Azure DevOps

Published: Thursday, July 14, 2022

Greetings, friends! I recently faced an issue where I couldn't install packages from the npm package registry stored in Azure Artifacts. I kept getting this error:

Copied! ⭐️
npm ERR! code E401
npm ERR! Unable to authenticate, need: Basic realm=""

I was scratching my head, wondering why the app couldn't reach out to the Azure Artifacts feed specified in the .npmrc file. Then, it dawned on me that my Personal Access Token (PAT) expired! We need to refresh the token or change the expiration date.

If you're using someone else's PAT, then you'll need to reach out to them to get the PAT refreshed. Just send them this tutorial 😎. Hopefully, the methods mentioned in this tutorial still work at the time you're reading this 😬.

On Windows, we can use the vsts-npm-auth CLI tool to force the PAT to be refreshed.

Copied! ⭐️
npm i -g vsts-npm-auth
vsts-npm-auth -config .npmrc -force

After running these two commands, the issue should hopefully be resolved. However, I was using a Mac, so the vsts-npm-auth CLI tool didn't work. This tool uses an executable that only works on Windows.

Since we can't use the CLI tool on Macs, we can login to the Azure DevOps web app and update the PAT's expiry date from there. Navigate to following URL to see a list of PATs you have created:

Copied! ⭐️

Make sure to replace YOUR_ORGANIZATION with the name of your organization. After you successfully navigate to the URL, you'll see a list of PATs you have created. Click on the PAT that expired. You should now see four options near the top of the page:

  • New Token
  • Revoke
  • Edit
  • Regenerate

Choose the Edit option, and a menu should appear. From this menu, we can change the expiration date of the PAT. Once you've updated the expiration date, click on Save, and your PAT should be working again!

Go back to your app and try to run npm install and hopefully it works! The error should be gone!

If you happened to hit the Regenerate option to regenerate your PAT, you may need to update your .npmrc file with the new PAT. That's why it might be easier to simply change the expiration date of an existing PAT.

More information can be found on Azure DevOp's documentation.