|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: How to publish an npm GitHub Package |
| 4 | +date: 2026-02-28 18:24:21 |
| 5 | +excerpt: How to publish an npm GitHub Package. |
| 6 | +categories: github packages npm publish |
| 7 | +--- |
| 8 | + |
| 9 | +This post goes over how to publish an npm [GitHub Package](https://docs.github.com/packages). |
| 10 | + |
| 11 | +## Publish |
| 12 | + |
| 13 | +Publish your npm package with GitHub Actions: |
| 14 | + |
| 15 | +{% raw %} |
| 16 | + |
| 17 | +```yml |
| 18 | +# .github/workflows/release.yml |
| 19 | +# ... |
| 20 | +jobs: |
| 21 | + publish: |
| 22 | + runs-on: ubuntu-latest |
| 23 | + permissions: |
| 24 | + contents: read |
| 25 | + packages: write |
| 26 | + |
| 27 | + steps: |
| 28 | + - name: Checkout repository |
| 29 | + uses: actions/checkout@v6 |
| 30 | + |
| 31 | + - name: Use Node.js |
| 32 | + uses: actions/setup-node@v6 |
| 33 | + with: |
| 34 | + registry-url: 'https://npm.pkg.github.com' |
| 35 | + scope: '@your-org' # <- update with your org or username |
| 36 | + |
| 37 | + - name: Install dependencies |
| 38 | + run: npm ci --prefer-offline |
| 39 | + |
| 40 | + - name: Publish package |
| 41 | + run: npm publish |
| 42 | + env: |
| 43 | + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 44 | +``` |
| 45 | +
|
| 46 | +{% endraw %} |
| 47 | +
|
| 48 | +See [example workflow](https://github.com/remarkablemark/github-package-test/blob/master/.github/workflows/release-please.yml). |
| 49 | +
|
| 50 | +## Install |
| 51 | +
|
| 52 | +To install the npm GitHub Package, update `.npmrc`: |
| 53 | + |
| 54 | +``` |
| 55 | +@your-org:registry=https://npm.pkg.github.com |
| 56 | +//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN} |
| 57 | +``` |
| 58 | +
|
| 59 | +> Replace `your-org` with your org or username. |
| 60 | +
|
| 61 | +Create auth [token](https://github.com/settings/tokens) with scope `read:packages` and add it to your shell config (e.g., `.zshrc`): |
| 62 | +
|
| 63 | +```bash |
| 64 | +export GITHUB_TOKEN=ghp_xxx |
| 65 | +``` |
| 66 | + |
| 67 | +Or do with with [GitHub CLI](https://cli.github.com/): |
| 68 | + |
| 69 | +```sh |
| 70 | +gh auth refresh -h github.com -s read:packages |
| 71 | +``` |
| 72 | + |
| 73 | +```sh |
| 74 | +echo 'export GITHUB_TOKEN=$(gh auth token)' >> ~/.zshrc |
| 75 | +``` |
| 76 | + |
| 77 | +Now you can install the package! |
| 78 | + |
| 79 | +See [example repo](https://github.com/remarkablemark/github-package-test). |
0 commit comments