Hello everyone I am trying to publish my NPM package to GitHub packages using this yaml:
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
name: Node.js Package (GHP)
on:
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: npm ci
- run: npm test
publish-gpr:
needs: build
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
registry-url: https://npm.pkg.github.com/
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
But I am getting this error which I don’t really understand since this is a template by GitHub themselves.
Here You can find my package.json.
I also tried adding a .npmrc file but my workflow still doesn’t work.
I am really hoping you guys can help me with this issue.
(My publish to NPM workflow works just fine btw, I just can’t seem to get it to work with GitHub Packages)
Edit:
My NPM account is also linked with GitHub.
2
Answers
ENEEEDAUTH
error while trying to publish an NPM package to GitHub Packages, it typically indicates an authentication issue (as illustrated bynpm/cli
issue 6184).If this does not apply, the rest of your GitHub Actions workflow seems correctly set up for publishing to GitHub Packages, with the
NODE_AUTH_TOKEN
environment variable being correctly assigned tosecrets.GITHUB_TOKEN
.The
.npmrc
(Roo7K1d:registry=https://npm.pkg.github.com
) (before you deleted it) tried to associate your package scope with GitHub’s npm registry. However, for it to work correctly, you need to make sure that the scope in yourpackage.json
matches the username or organization name on GitHub exactly and is correctly referenced in.npmrc
. For GitHub Packages, the correct format usually includes the scope, like:And your
package.json
name field should include the scope as well:That is important for GitHub Packages because it uses the scope to route the package to your account. Your
publishConfig
section seems correctly set, but make sure the scope is included in your package name.Double-check that your GitHub token has the appropriate permissions for package publication.
IIRC
npm publish
fails when the package version is already in the registry. TheJS-DevTools/npm-publish
action can handle such cases without throwing errors:This way you can control to which registries your package should be published.
I use this in all my workflows