Skip to content

Commit eff0304

Browse files
authored
Merge branch 'main' into repo-sync
2 parents 1662432 + 3aad784 commit eff0304

File tree

1 file changed

+10
-64
lines changed

1 file changed

+10
-64
lines changed

content/actions/publishing-packages/publishing-nodejs-packages.md

Lines changed: 10 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ shortTitle: Node.js packages
2424

2525
## Introduction
2626

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.
2828

2929
## Prerequisites
3030

@@ -55,15 +55,15 @@ Each time you create a new release, you can trigger a workflow to publish your p
5555

5656
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)."
5757

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`.
5959

6060
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.
6161

6262
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.
6363

6464
{% raw %}
6565
```yaml{:copy}
66-
name: Node.js Package
66+
name: Publish Package to npmjs
6767
on:
6868
release:
6969
types: [created]
@@ -75,7 +75,7 @@ jobs:
7575
# Setup .npmrc file to publish to npm
7676
- uses: actions/setup-node@v2
7777
with:
78-
node-version: '12.x'
78+
node-version: '16.x'
7979
registry-url: 'https://registry.npmjs.org'
8080
- run: npm ci
8181
- run: npm publish
@@ -92,6 +92,8 @@ registry=https://registry.npmjs.org/
9292
always-auth=true
9393
```
9494

95+
Please note that you need to set the `registry-url` to `https://registry.npmjs.org/` in `setup-node` to properly configure your credentials.
96+
9597
## Publishing packages to {% data variables.product.prodname_registry %}
9698

9799
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
122124
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.
123125

124126
```yaml{:copy}
125-
name: Node.js Package
127+
name: Publish package to GitHub Packages
126128
on:
127129
release:
128130
types: [created]
@@ -137,7 +139,7 @@ jobs:
137139
# Setup .npmrc file to publish to GitHub Packages
138140
- uses: actions/setup-node@v2
139141
with:
140-
node-version: '12.x'
142+
node-version: '16.x'
141143
registry-url: 'https://npm.pkg.github.com'
142144
# Defaults to the user or organization that owns the workflow file
143145
scope: '@octocat'
@@ -161,7 +163,7 @@ If you use the Yarn package manager, you can install and publish packages using
161163

162164
{% raw %}
163165
```yaml{:copy}
164-
name: Node.js Package
166+
name: Publish Package to npmjs
165167
on:
166168
release:
167169
types: [created]
@@ -173,7 +175,7 @@ jobs:
173175
# Setup .npmrc file to publish to npm
174176
- uses: actions/setup-node@v2
175177
with:
176-
node-version: '12.x'
178+
node-version: '16.x'
177179
registry-url: 'https://registry.npmjs.org'
178180
# Defaults to the user or organization that owns the workflow file
179181
scope: '@octocat'
@@ -183,59 +185,3 @@ jobs:
183185
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
184186
```
185187
{% 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
236-
scope: '@octocat'
237-
# Publish to GitHub Packages
238-
- run: npm publish
239-
env:
240-
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}{% endraw %}
241-
```

0 commit comments

Comments
 (0)