W3cubDocs

/npm

Using private packages in a CI/CD workflow

Skip to content

You can use access tokens to test private npm packages with continuous integration (CI) systems, or deploy them using continuous deployment (CD) systems.

Create a new access token

Create a new access token that will be used only to access npm packages from a CI/CD server.

Continuous integration

By default, npm token create will generate a token with both read and write permissions. When generating a token for use in a continuous integration environment, we recommend creating a read-only token:

npm token create --read-only

For more information on creating access tokens, including CIDR-whitelisted tokens, see "Creating an access token".

Continuous deployment

Since continuous deployment environments usually involve the creation of a deploy artifact, you may wish to create an automation token on the website. This will allow you to publish even if you have two-factor authentication enabled on your account.

Interactive workflows

If your workflow produces a package, but you publish it manually after validation, then you will want to create a token with read and write permissions, which are granted with the standard token creation command:

npm token create

CIDR whitelists

For increased security, you may use a CIDR-whitelisted token that can only be used from a certain IP address range. You can use a CIDR whitelist with a read and publish token or a read-only token:

npm token create --cidr=[list]
npm token create --read-only --cidr=[list]

Example:

npm token create --cidr=192.0.2.0/24

For more information, see "Creating and viewing authentication tokens".

Set the token as an environment variable on the CI/CD server

Set your token as an environment variable, or a secret, in your CI/CD server.

For example, in GitHub Actions, you would add your token as a secret. Then you can make the secret available to workflows.

If you named the secret NPM_TOKEN, then you would want to create an environment variable named NPM_TOKEN from that secret.

steps:
  - run: |
      npm install
  - env:
      NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

Consult your CI/CD server's documentation for more details.

Create and check in a project-specific .npmrc file

Use a project-specific .npmrc file with a variable for your token to securely authenticate your CI/CD server with npm.

  1. In the root directory of your project, create a custom .npmrc file with the following contents:

    //registry.npmjs.org/:_authToken=${NPM_TOKEN}

    Note: that you are specifying a literal value of ${NPM_TOKEN}. The npm cli will replace this value with the contents of the NPM_TOKEN environment variable. Do not put a token in this file.

  2. Check in the .npmrc file.

Securing your token

Your token may have permission to read private packages, publish new packages on your behalf, or change user or package settings. Protect your token.

Do not add your token to version control or store it insecurely. Store it in a password manager, your cloud provider's secure storage, or your CI/CD provider's secure storage.

© npm, Inc. and Contributors
Licensed under the npm License.
npm is a trademark of npm, Inc.
https://docs.npmjs.com/using-private-packages-in-a-ci-cd-workflow