Search code examples
node.jsnpmgithub-actionsnpm-publishgithub-package-registry

GitHub actions: NPM publish fails with ERR! code ENEEDAUTH


I've attempted to implement the official guide to publishing and installing a package with GitHub Actions: Authenticating to package registries with granular permissions

Fails with:

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

package.json

{
  "name": "@charneykaye/banana",
  "version": "4.0.6",
  "repository": "[email protected]:charneykaye/banana",
  "description": "made by artists in a new algorithmic medium",
  "bin": {
    "banana": "./lib/index.js"
  },
  "author": "Charney Kaye <[email protected]>",
  "license": "MIT",
  "scripts": {
    "start": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts",
    "start:windows": "nodemon --watch 'src/**/*.ts' --exec \"npx ts-node\" src/index.ts",
    "create": "npm run build && npm run test",
    "banana": "npx ts-node ./src/index.ts",
    "test": "tsc -p . && jest --coverage --verbose --runInBand"
  },
  "dependencies": {
    "commander": "^10.0.0",
    "figlet": "^1.5.2",
    "octokit": "^1.8.0"
  },
  "devDependencies": {
    "@babel/core": "^7.20.12",
    "@babel/preset-env": "^7.20.2",
    "@babel/preset-typescript": "^7.18.6",
    "@jest/globals": "^29.4.1",
    "@types/jest": "^29.4.0",
    "@types/node": "^18.11.18",
    "babel-jest": "^29.4.1",
    "jest": "^29.4.1",
    "nodemon": "^2.0.20",
    "ts-jest": "^29.0.5",
    "ts-node": "^10.9.1",
    "typescript": "^4.9.5"
  },
  "publishConfig": {
    "registry": "https://npm.pkg.github.com"
  },
  "jest": {
    "preset": "ts-jest",
    "testEnvironment": "node",
    "testMatch": [
      "**/__tests__/**test.ts",
      "**/__tests__/**test.tsx"
    ]
  }
}

.github/workflow/ci.yml

name: "CI Build & Publish"

on:
  push:
    branches:
      - main

jobs:
  CI:
    runs-on: ubuntu-latest

    steps:

      - name: Checkout
        uses: actions/checkout@v2

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 18.14
          cache: 'npm'

      - name: Install npm packages
        run: npm install

      - name: Unit tests
        run: npm test

      - name: Build Banana
        run: npm run banana -- --build --env prod

      - uses: actions/upload-artifact@v3
        with:
          name: banana
          path: ./build/

      - name: Publish NPM package
        run: npm publish

.npmrc

@charneykaye:registry=https://npm.pkg.github.com

Solution

  • If you are using the setup-node github action, make sure that you are defining the registry-url (this is required even for publishing to npm)

    for example

          - name: set node-version
            uses: actions/setup-node@v3
            with:
              registry-url: 'https://registry.npmjs.org/'
              node-version-file: '.nvmrc'
              cache: 'npm'