Nullndr
Nullndr

Reputation: 1827

Unable to publish an npm package with github actions

I'm trying to publish my npm package both on github register and npm register via a github action. Success with the github one, but the other one is failing with the following:

npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in to https://npm.pkg.github.com/nullndr
npm ERR! need auth You need to authorize this machine using `npm adduser`

Why do I need to npm adduser. I use an automation token, is not it enough?

here is my workflow file:

# 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://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: Node.js Package

on:
  release:
    types: [created]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16
      - run: npm ci

  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: 16
          registry-url: https://npm.pkg.github.com/
          scope: "@nullndr"
      - run: npm install
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.PUBLISH_GITHUB_TOKEN }}

  publish-npm:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16
          registry-url: https://registry.npmjs.org/
      - run: npm install
      - run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.PUBLISH_NPM_TOKEN }}

What am I missing?

Upvotes: 10

Views: 2771

Answers (2)

izzqz
izzqz

Reputation: 48

I don't know why. But in workflows, npm prefers only publishConfig in package.json for it.

I trick it using jq in job.

- name: Publish to github registry
  run: |
    # Change publish registry
    echo "$(jq '.publishConfig.registry = "https://npm.pkg.github.com"' package.json)" > package.json
    # Add organisation scope to package name
    echo "$(jq '.name = "@scope/package"' package.json)" > package.json
    # And publish this boy
    npm publish --@scope:registry=https://npm.pkg.github.com

My full workflow file which publish package to both registries: https://github.com/zenfuse/zenfuse.js/blob/21de49de28ab99cb8634e3b0c4bf30078f80eab3/.github/workflows/package-publish.yml

Upvotes: 1

Fer Olivero
Fer Olivero

Reputation: 11

Check if PUBLISH_NPM_TOKEN appears in your repository environment variables.

Adding PUBLISH_NPM_TOKEN to Environment variables will not work. You need to add it as repository environment.

Repository Environments

Upvotes: 1

Related Questions