How to Publish an npm Package to GitHub Packages
Set the registry to npm.pkg.github.com scoped to your org and authenticate with the built-in GITHUB_TOKEN, so no separate publish secret is needed.
GitHub Packages hosts npm packages under npm.pkg.github.com. The package name must be scoped to the owner (for example @my-org/pkg). CI authenticates with the automatically provided GITHUB_TOKEN plus packages: write permission.
Steps
- Scope the package name to the owner, e.g.
@my-org/toolinpackage.json. - Set
registry-urltohttps://npm.pkg.github.cominsetup-node. - Grant
packages: writeand publish withNODE_AUTH_TOKENset toGITHUB_TOKEN.
Workflow
.github/workflows/ci.yml
permissions:
contents: read
packages: write
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: 'https://npm.pkg.github.com'
scope: '@my-org'
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}.npmrc
.npmrc
@my-org:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}Gotchas
- The scope in
package.jsonmust match the GitHub owner or publish returns404 Not Found. - Consumers also need a
.npmrcline mapping the scope tonpm.pkg.github.com.
Related guides
How to Publish an npm Package From CIPublish an npm package from CI with npm publish and an NPM_TOKEN automation token, setting registry auth in .…
How to Publish a Maven Artifact to GitHub PackagesPublish a Maven artifact to GitHub Packages from CI by adding a distributionManagement repository and a serve…
How to Configure a Scoped Registry in .npmrc for PublishingRoute a scoped npm package to a specific registry in CI with an .npmrc that maps the scope and its auth token…