Azure DevOps onPremise npm registry access in different collection via pipeline

Viewed 1166

We use Azure DevOps onPremise (Azure DevOps Server 2020 Update 1). There we have two collections. "Libraries" and "Projects".

The "Libraries" collection contains a project that hosts an npm artifact feed. We can run a pipeline in the "Libraries" project and access packages from the feed (e.g. via npm install).

Additonally, we have a pipeline in a "Projects" collection project, were we created a "Service Connection" ('iconenpm') to the "Libraries" npm feed using a PAT. (This PAT was created for the pipeline user and is BASE64 encoded). The PAT is also added to the pipeline user's %userprofile%\.npmrc file.

However, we are not able to successfully run an npm install. This is the task:

  - task: Npm@1
    displayName: 'npm install'
    inputs:
      command: 'install'
      customEndpoint: 'iconenpm'

UI task setting

and 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

...
additonal log output
...

2085 error code E401
2086 error Unable to authenticate, your authentication token seems to be invalid.

One note: when there is no package from our custom feed in the package.json, the pipeline works fine. As soon as we want to access some package via the Service Connection, the pipeline fails.

Does anyone have some suggestions?

2 Answers

If you see the error it says to try npm login. But I would recommend not to try npm login as it logs you in the npmjs.com website or your default registry, not in your Azure DevOps organization.

As given in this document.

If you are developing on Windows, it is recommended that you use vsts-npm-auth to fetch the credentials and inject them into your %USERPROFILE%.npmrc. The easiest way to set this up is to install vsts-npm-auth globally and then add a run script to your package.json.

But if you are developing in any other platform following these steps mention this document to correctly setup the credentials.

If after setting up the credentials correctly you are facing the same issue then, I would suggest to follow the steps mentioned below to resolve the problem.

  1. Check if token is expired

    Maybe the token is expired. Simply navigate to Azure DevOps and generate new credentials and store it in .npmrc file at user level.

  2. Refresh the token

    Inside your project, you can open a terminal and run vsts-npm-auth -F -C .npmrc. This script refreshes the token.

  3. Check if URL contains space

    Check if the URLs in .npmrc file at user level contain space then, replace every space with %20. I found that when trying to authenticate, if the Artifact Page URL and .npmrc file URLs somehow don't match because of that space then, it throws the 401 error.

I would suggest to read this troubleshooting section of Microsoft document and this How to connect and authenticate to NPM feeds on Azure DevOps document for more information.

I faced this issue and I encountered the problems below :

I have a project from Azure DevOps Collection A that have to access to the private NPM feed from Collection B.

In the .npmrc in your repo, you must double check that the registry is correctly named :

registry=https://registry.npmjs.com
@collectionB:registry=https://mydevopsserver.x.y/collectionB/_packaging/collectionB/npm/registry/
always-auth=true
; auth token below..

Something like %40Local should not be in the url. That was my first issue.

registry=https://registry.npmjs.com
@collectionB:registry=https://mydevopsserver.x.y/collectionB/_packaging/collectionB%40Local/npm/registry/
always-auth=true
; auth token below..

I also found another issue, as I created a demo repo to try to reproduce the error, I forgot to add package-lock.json which was causing the same issue!

Finally, I also found a workaround, you could add a custom npm install step by forcing a specific .npmrc from the server for example :

npm install --userconfig c:\Users\userX\.npmrc
Related