You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/actions/publishing-packages/publishing-nodejs-packages.md
+10-64Lines changed: 10 additions & 64 deletions
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ shortTitle: Node.js packages
24
24
25
25
## Introduction
26
26
27
-
This guide shows you how to create a workflow that publishes Node.js packages to the {% data variables.product.prodname_registry %} and npm registries after continuous integration (CI) tests pass. With a single workflow, you can publish packages to a single registry or multiple registries.
27
+
This guide shows you how to create a workflow that publishes Node.js packages to the {% data variables.product.prodname_registry %} and npm registries after continuous integration (CI) tests pass.
28
28
29
29
## Prerequisites
30
30
@@ -55,15 +55,15 @@ Each time you create a new release, you can trigger a workflow to publish your p
55
55
56
56
To perform authenticated operations against the npm registry in your workflow, you'll need to store your npm authentication token as a secret. For example, create a repository secret called `NPM_TOKEN`. For more information, see "[Creating and using encrypted secrets](/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets)."
57
57
58
-
By default, npm uses the `name` field of the *package.json* file to determine the npm registry. When publishing to a global namespace, you only need to include the package name. For example, you would publish a package named `npm-hello-world-test` to the`https://www.npmjs.com/package/npm-hello-world-test`.
58
+
By default, npm uses the `name` field of the *package.json* file to determine the name of your published package. When publishing to a global namespace, you only need to include the package name. For example, you would publish a package named `npm-hello-world-test` to `https://www.npmjs.com/package/npm-hello-world-test`.
59
59
60
60
If you're publishing a package that includes a scope prefix, include the scope in the name of your *package.json* file. For example, if your npm scope prefix is octocat and the package name is hello-world, the `name` in your *package.json* file should be `@octocat/hello-world`. If your npm package uses a scope prefix and the package is public, you need to use the option `npm publish --access public`. This is an option that npm requires to prevent someone from publishing a private package unintentionally.
61
61
62
62
This example stores the `NPM_TOKEN` secret in the `NODE_AUTH_TOKEN` environment variable. When the `setup-node` action creates an *.npmrc* file, it references the token from the `NODE_AUTH_TOKEN` environment variable.
Please note that you need to set the `registry-url` to `https://registry.npmjs.org/` in `setup-node` to properly configure your credentials.
96
+
95
97
## Publishing packages to {% data variables.product.prodname_registry %}
96
98
97
99
Each time you create a new release, you can trigger a workflow to publish your package. The workflow in the example below runs anytime the `release` event with type `created` occurs. The workflow publishes the package to {% data variables.product.prodname_registry %} if CI tests pass.
@@ -122,7 +124,7 @@ If you want to publish your package to a different repository, you must use a pe
122
124
This example stores the `GITHUB_TOKEN` secret in the `NODE_AUTH_TOKEN` environment variable. When the `setup-node` action creates an *.npmrc* file, it references the token from the `NODE_AUTH_TOKEN` environment variable.
123
125
124
126
```yaml{:copy}
125
-
name: Node.js Package
127
+
name: Publish package to GitHub Packages
126
128
on:
127
129
release:
128
130
types: [created]
@@ -137,7 +139,7 @@ jobs:
137
139
# Setup .npmrc file to publish to GitHub Packages
138
140
- uses: actions/setup-node@v2
139
141
with:
140
-
node-version: '12.x'
142
+
node-version: '16.x'
141
143
registry-url: 'https://npm.pkg.github.com'
142
144
# Defaults to the user or organization that owns the workflow file
143
145
scope: '@octocat'
@@ -161,7 +163,7 @@ If you use the Yarn package manager, you can install and publish packages using
161
163
162
164
{% raw %}
163
165
```yaml{:copy}
164
-
name: Node.js Package
166
+
name: Publish Package to npmjs
165
167
on:
166
168
release:
167
169
types: [created]
@@ -173,7 +175,7 @@ jobs:
173
175
# Setup .npmrc file to publish to npm
174
176
- uses: actions/setup-node@v2
175
177
with:
176
-
node-version: '12.x'
178
+
node-version: '16.x'
177
179
registry-url: 'https://registry.npmjs.org'
178
180
# Defaults to the user or organization that owns the workflow file
179
181
scope: '@octocat'
@@ -183,59 +185,3 @@ jobs:
183
185
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
184
186
```
185
187
{% endraw %}
186
-
187
-
## Publishing packages to npm and {% data variables.product.prodname_registry %}
188
-
189
-
{% note %}
190
-
191
-
**Note:** If you need to publish to registries that have different scope prefixes, you'll need to modify the *package.json* file on the runner to change the scope prefix. For example, if you publish a package to the `@mona` scope for npm and `@octocat` scope for {% data variables.product.prodname_registry %}, you can replace the `@mona` scope with `@octocat` in the *package.json* file on the runner after publishing to npm and before publishing to {% data variables.product.prodname_registry %}.
192
-
193
-
{% endnote %}
194
-
195
-
You can publish your packages to both the npm registry and {% data variables.product.prodname_registry %} by using the `setup-node` action for each registry.
196
-
197
-
If you publish a package to both registries, you'll need to ensure that your scope prefix on npm matches your {% data variables.product.prodname_dotcom %} user or organization name. To publish packages to a public registry with a scope prefix, you can use the command `npm publish --access public`. For more information, see [`npm-scope`](https://docs.npmjs.com/misc/scope) and "[Creating and publishing scoped public packages](https://docs.npmjs.com/creating-and-publishing-scoped-public-packages)" in the npm documentation.
198
-
199
-
Ensure your *package.json* file includes the scope of your {% data variables.product.prodname_dotcom %} repository and npm registry. For example, if you plan to publish a package in the `octocat/npm-hello-world-test` repository to {% data variables.product.prodname_dotcom %} and https://www.npmjs.com/package/@octocat/npm-hello-world-test, the name in your *package.json* file would be `"name": "@octocat/npm-hello-world-test"`.
200
-
201
-
To perform authenticated operations against the {% data variables.product.prodname_registry %} registry in your workflow, you can use the `GITHUB_TOKEN`. {% data reusables.github-actions.github-token-permissions %}
202
-
203
-
When you use the `scope` input to the `setup-node` action, the action creates an *.npmrc* file that includes the scope prefix. By default, the `setup-node` action sets the scope in the *.npmrc* file to the user or organization that owns the workflow file.
204
-
205
-
This workflow calls the `setup-node` action two times. Each time the `setup-node` action runs, it overwrites the *.npmrc* file. The *.npmrc* file references the token that allows you to perform authenticated operations against the package registry from the `NODE_AUTH_TOKEN` environment variable. The workflow sets the `NODE_AUTH_TOKEN` environment variable each time the `npm publish` command is run, first with a token to publish to npm (`NPM_TOKEN`) and then with a token to publish to {% data variables.product.prodname_registry %} (`GITHUB_TOKEN`).
206
-
207
-
208
-
```yaml{:copy}
209
-
name: Node.js Package
210
-
on:
211
-
release:
212
-
types: [created]
213
-
jobs:
214
-
build:
215
-
runs-on: ubuntu-latest {% ifversion fpt or ghes > 3.1 or ghae or ghec %}
216
-
permissions:
217
-
contents: read
218
-
packages: write {% endif %}
219
-
steps:
220
-
- uses: actions/checkout@v2
221
-
# Setup .npmrc file to publish to npm
222
-
- uses: actions/setup-node@v2
223
-
with:
224
-
node-version: '10.x'
225
-
registry-url: 'https://registry.npmjs.org'
226
-
- run: npm ci
227
-
# Publish to npm
228
-
- run: npm publish --access public
229
-
env:{% raw %}
230
-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
231
-
# Setup .npmrc file to publish to GitHub Packages
232
-
- uses: actions/setup-node@v2
233
-
with:
234
-
registry-url: 'https://npm.pkg.github.com'
235
-
# Defaults to the user or organization that owns the workflow file
0 commit comments