Set up your client's npmrc

VSTS | TFS 2018 | TFS 2017

All Package Management feeds require authentication, so you'll need to store credentials for the feed before you can install or publish packages. npm uses .npmrc configuration files to store feed URLs and credentials.

Where are my .npmrc files?

VSTS recommends using two .npmrc files:

  1. One .npmrc should live at the root of your git repo adjacent to your project's package.json. It should contain a "registry" line for your feed and it should not contain credentials since it will be checked into git. You can find the registry information for your feed from the Connect to Feed button:

    1. From your Packages page, click Connect to Feed

      Connect to feed from VSTS Package Management

    2. Copy the "registry" text:

      Connect to feed from VSTS Package Management

  2. On your development machine, you will also have a .npmrc in $home for Linux or Mac systems or $env.HOME for win systems. This .npmrc should contain credentials for all of the registries that you need to connect to. The NPM client will look at your project's .npmrc, discover the registry, and fetch matching credentials from $home/.npmrc or $env.HOME/.npmrc. Credential acquisition will be discussed in the next section.

This enables you to share project's .npmrc with the whole team while keeping your credentials secure.

Set up authentication on your dev box

You should have a project specific .npmrc containing only your Feed's registry information that you discovered from the "Connect to Feed" dialog. There should be no credentials in this file and the file itself is usually adjacent to your project's package.json.

IMPORTANT: There can only be a single "registry=" line in your .npmrc. Multiple registries are possible with scopes and our new upstream sources feature.

Windows

If you are developing on Windows, we recommend that you use vsts-npm-auth to fetch credentials and inject them into your ~/.npmrc on a periodic basis. The easiest way to set this up is to install vsts-npm-auth globally (i.e. npm install -g vsts-npm-auth) and then add a run script in your project's package.json.

"scripts": {
    "refreshVSToken" : "vsts-npm-auth -config .npmrc"
}

Linux or Mac

If you are developing on Linux or Mac, vsts-npm-auth is not supported and we recommend generating a token in the following manner for your $HOME/.npmrc

For Linux or Mac users, the Connect to feed dialog will generate an appropriately-formatted token that you can place into your .npmrc file with a lifespan of 90 days.

If you want to create a token that lasts longer than 90 days, skip to the second method below.

90-day token:

  1. From the Packages hub, select Connect to feed

  2. Select npm.

  3. Click Generate npm credentials and copy them to add them to your user .npmrc manually:

    Connect to feed from VSTS Package Management Linux/Mac credentials

Create a token that lasts longer than 90 days:

  1. Navigate to security and generate a PAT with a narrow scope of "Packaging (read and write)".
  2. Base64 encode the PAT. On Windows you can use...

    [Convert]::ToBase64String([system.Text.Encoding]::UTF8.GetBytes("YOUR_PAT_GOES_HERE"))
    
  3. In your $home/.npmrc add the following lines replacing account, feedname, username, PAT, and email.

    //<youraccount>.pkgs.visualstudio.com/_packaging/<yourfeed>/npm/registry/:username=YOUR-USERNAME
    //<youraccount>.pkgs.visualstudio.com/_packaging/<yourfeed>/npm/registry/:_password=BASE64-ENCODED-PAT-GOES-HERE
    //<youraccount>.pkgs.visualstudio.com/_packaging/<yourfeed>/npm/registry/:email=YOUREMAIL@EXAMPLE.COM
    //<youraccount>.pkgs.visualstudio.com/_packaging/<yourfeed>/npm/registry/:always-auth=true
    

Set up authentication in a build task

There are two options for setting up authentication in a build task:

Without a Task Runner

To set up npm authentication in a build task without a task runner, follow the directions below.

  1. Add a build definition in VSTS under the Build and Release --> Builds hub.

    Connect to feed from VSTS Package Management

  2. Choose your source Team project, Repository, and Default branch and select Continue

  3. Select Empty process at the top of the form

  4. Add a task to Phase 1 of your build definition by clicking the "+":

    Add task to build definition

  5. Select Package or search for npm in the search bar, select npm and select Add:

    Add task to build definition

  6. Select the npm install task underneath Phase 1:

    Add task to build definition

  7. Browse to and select your Working folder with package.json:

    Add task to build definition

  8. Expand Custom registries and authentication, here you have a few options:

    • Registries in my .npmrc

      Add task to build definition

      You can choose credentials to authenticate to outside of your current account/collection by setting up service endpoints.

    • Registry I select here

      Add task to build definition

      When you choose this option, the task will create a temporary .npmrc with credentials for the registry you've selected and it will override the project's .npmrc. This is useful when you want to publish to a specific feed.

With a Task Runner (e.g. make gulp work)

When using a task runner, you'll need to add the npm Authenticate build task at the beginning of your build definition. This will inject credentials into your proejct's .npmrc and persist them for the lifespan of the build. This allows subsequent build steps to use the credentials in the .npmrc.

  1. Add a build definition in VSTS under the Build and Release --> Builds hub.

    Connect to feed from VSTS Package Management

  2. Choose your source Team project, Repository, and Default branch and select Continue

  3. Select Empty process at the top of the form

  4. Add a task to Phase 1 of your build definition by clicking the "+":

    Add task to build definition

  5. Select Package or search for npm in the search bar, select npm Authenticate and select Add:

    Add task to build definition

  6. Select the npm Authenticate task underneath Phase 1:

    Add task to build definition

  7. Browse to and select your .npmrc file to authenticate:

    Add task to build definition

    You can choose credentials to authenticate to outside of your current account/collection by setting up service endpoints.

  8. After setting up your npm Authenticate task, you can add other build task(s) for your task runner like Gulp.

Troubleshooting vsts-npm-auth

If you receive an error like:

  • Command Prompt: '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, re-run Node.js setup and ensure the Add to PATH option and its child options are selected for installation.

Add to PATH install option in Node.js setup

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