Set up your project and connect to Azure Artifacts

Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019 | TFS 2018

With Azure Artifacts, you can publish different types of packages to your feeds such as npm, NuGet, Python, Maven, and Universal packages. You can also install packages from feeds and public registries such as npmjs.com. To authenticate with Azure Artifacts, we must first set up our config file. Npm uses .npmrc configuration files to store feed URLs and credentials.

Project setup

We recommend using two config files, the first one you should use to authenticate to Azure Artifacts, and the second one should be kept locally to store your credentials. On your development machine, place the second .npmrc file in your home directory. This second file should contain all your registries' credentials. The following steps will help set up your other config file:

  1. Select Artifacts, and then select Connect to feed.

    Screenshot showing how to connect to a feed.

  1. Select Packages, and then select Connect to feed.

    Screenshot showing how to connect to a feed in TFS.

  1. Select npm from the list of package types.

  2. If this is the first time using Azure Artifacts with npm, select Get the tools and follow the instructions to install the prerequisites.

  1. Follow the instructions in Project setup to set up your config file.

    Screenshot showing the steps to set up the project and publish and restore packages.

  1. Follow the instructions in Project setup to set up your config file.

    Screenshot showing the steps to set up the project and restore packages.

  1. Follow the instructions to set up your project.

    Screenshot showing the steps to set up the project in TFS.

Note

if your organization is using a firewall or a proxy server, make sure you allow the appropriate domain URLs. See Allowed IP addresses and domain URLs for more details.

Credentials setup

Tip

Multiple registries in .npmrc files are supported with upstream sources and scopes.

If you're developing on Windows, we recommend 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.

  • Install vsts-npm-auth globally:

    npm install -g vsts-npm-auth
    
  • Add script to package.json:

    "scripts": {
        "refreshVSToken" : "vsts-npm-auth -config ".\.npmrc\" -TargetConfig "$HOME\.npmrc\""
    }
    

Note

vsts-npm-auth is not supported in TFS and Azure DevOps Server.

Pipeline authentication

Azure Artifacts recommend using the npm authenticate task to authenticate with your pipeline. When using a task runner such as gulp or Grunt, you'll need to add the npm authenticate task at the beginning of your pipeline. This will inject your credentials into your project's .npmrc and persist them for the lifespan of the pipeline run. This allows subsequent steps to use the credentials in the config file.

  1. Select Azure Pipelines, and then select your pipeline definition.

  2. Select Edit to modify your pipeline.

  3. Select + to add a task to your pipeline.

    Screenshot showing how to add the npm authenticate task to your pipeline

  4. Search for the npm Authenticate task, and then select Add to add it to your pipeline.

    Screenshot showing the npm authenticate task added to the pipeline

  5. Select your .npmrc file.

    Screenshot showing how to add your .npmrc file

  6. Select Save & queue when you're done.

  1. Select Build and Release, and then select Builds.

    Screenshot showing how to access your builds in TFS

  2. Select your pipeline, and then select Edit.

  3. Select + to add a task to your pipeline.

    Screenshot showing how to add a new task to your pipeline

  4. Search for the npm Authenticate task, and then select Add to add it to your pipeline.

    Screenshot showing the npm authenticate task

  5. Select your .npmrc file.

    Screenshot showing how to add your .npmrc file to the npm authenticate task

  6. Select Save & queue when you're done.

Note

To grant permissions to your pipeline, make sure you set the build service role to Contributor in your feed settings.

A screenshot showing the build service roles in feed settings.

Troubleshoot

vsts-npm-auth is not recognized

If you're running into the following error:

  • Cmd: 'vsts-npm-auth' is not recognized as an internal or external command, operable program or batch file.
  • PowerShell: vsts-npm-auth : The term 'vsts-npm-auth' is not recognized as the name of a cmdlet, function, script file, or operable program.

Then it's likely that the npm modules folder is not in your path. To fix this issue, rerun the Node.js setup and make sure that the Add to PATH options are selected.

Screenshot showing how to set up node.js

Alternatively, you can edit the PATH variable %APPDATA%\npm (Command Prompt) or $env:APPDATA\npm (PowerShell) to add it to your path.

Unable to authenticate

If you're running into a E401 error: code E401 npm ERR! Unable to authenticate. Run the vsts-npm-auth command with -F flag to reauthenticate.

vsts-npm-auth -config .npmrc -F

Reset vsts-npm-auth

Follow the steps below to modify/reset your vsts-npm-auth credentials:

  • Uninstall vsts-npm-auth.

    npm uninstall -g vsts-npm-auth
    
  • Clear your npm cache.

    npm cache clean --force
    
  • Delete your .npmrc file.

  • Reinstall vsts-npm-auth.

    npm install -g vsts-npm-auth --registry https://registry.npmjs.com --always-auth false