1 ---
2 title: Using private packages in a CI/CD workflow
3 redirect_from:
4 - /private-modules/ci-server-config
5 ---
6
7 You can use access tokens to test private npm packages with continuous integration (CI) systems, or deploy them using continuous deployment (CD) systems.
8
9 ## Recommended: Use trusted publishing for package publishing
10
11 For publishing packages from CI/CD workflows, we recommend using [trusted publishing](/trusted-publishers) instead of access tokens. Trusted publishing uses OpenID Connect (OIDC) to provide secure publishing that eliminates the security risks associated with long-lived tokens.
12
13 Trusted publishing is supported for:
14
15 - [GitHub Actions](https://github.com/features/actions) (GitHub-hosted runners)
16 - [GitLab CI/CD](https://docs.gitlab.com/ci/pipelines/) (GitLab.com shared runners)
17
18 If you use a different CI/CD provider, or if you need to install private packages (not publish), you can use access tokens as described below.
19
20 ## Create a new access token
21
22 Create a new access token that will be used only to access npm packages from a CI/CD server.
23
24 ### Continuous integration
25
26 When generating an access token for use in a continuous integration environment, we recommend using a granular access token with limited access to provide greater security. Granular access tokens allow you to restrict access to specific packages, set expiration dates, and configure IP restrictions.
27
28 If you use a legacy token instead, by default, `npm token create` will generate a token with both read and write permissions. We recommend creating a read-only token:
29
30 ```
31 npm token create --read-only
32 ```
33
34 For more information on creating access tokens, including CIDR-whitelisted tokens, see "[Creating an access token][create-token]".
35
36 ### Continuous deployment
37
38 For publishing packages in continuous deployment environments, we strongly recommend using [trusted publishing](/trusted-publishers) when available, as it provides enhanced security without requiring token management.
39
40 If trusted publishing is not available for your CI/CD provider, you may create a [granular access token][create-token] configured to bypass 2FA on the website. **Granular access tokens with 2FA bypass are the recommended replacement for legacy automation tokens**, providing the same 2FA bypass capability with additional security features like expiration dates, scope restrictions, and IP address limitations. Alternatively, you can use a legacy [automation token][create-token], though granular access tokens are recommended for enhanced security. Both token types will allow you to publish even if you have two-factor authentication enabled on your account.
41
42 ### Interactive workflows
43
44 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:
45
46 ```
47 npm token create
48 ```
49
50 ### CIDR whitelists
51
52 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:
53
54 ```
55 npm token create --cidr=[list]
56 npm token create --read-only --cidr=[list]
57 ```
58
59 Example:
60
61 ```
62 npm token create --cidr=192.0.2.0/24
63 ```
64
65 For more information, see "[Creating and viewing authentication tokens][create-token]".
66
67 ## Set the token as an environment variable on the CI/CD server
68
69 Set your token as an environment variable, or a secret, in your CI/CD server.
70
71 For example, in GitHub Actions, you would [add your token as a secret](https://docs.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets). Then you can make the secret available to workflows.
72
73 If you named the secret `NPM_TOKEN`, then you would want to create an environment variable named `NPM_TOKEN` from that secret.
74
75 ```
76 steps:
77 - run: |
78 npm install
79 - env:
80 NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
81 ```
82
83 Consult your CI/CD server's documentation for more details.
84
85 ## Create and check in a project-specific .npmrc file
86
87 Use a project-specific `.npmrc` file with a variable for your token to securely authenticate your CI/CD server with npm.
88
89 1. In the root directory of your project, create a custom `.npmrc` file with the following contents:
90
91 ```
92 //registry.npmjs.org/:_authToken=${NPM_TOKEN}
93 ```
94
95 **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.
96
97 2. Check in the `.npmrc` file.
98
99 ## Securing your token
100
101 Your token may have permission to read private packages, publish new packages on your behalf, or change user or package settings. Protect your token.
102
103 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.
104
105 When possible, use granular access tokens with the minimum permissions necessary, and set short expiration dates for your tokens. For more information, see "[About access tokens][about-tokens]."
106
107 [create-token]: creating-and-viewing-access-tokens
108 [about-tokens]: about-access-tokens