Github actions: NPM publish 404 not found

4.6k views Asked by At

In my github project Im trying to automatically create a new version and publish it to NPM whenever something is pushed to the master branch.

The idea

  1. Create a new minor version
  2. Publish the package to NPM

Im using github actions. My workflow file looks like this:

# This workflow will run tests using node and then publish a package to the npm registry 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:
#trigger on every commit to the main branch
  push:
    branches:
      - main
      - master 

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 12
      - run: npm test

  publish-npm:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 12
          registry-url: https://registry.npmjs.org/
      - run: git config user.name $GITHUB_ACTOR
      - run: git config user.email gh-actions-${GITHUB_ACTOR}@github.com
      - run: git remote add gh-origin https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git
      - run: echo "//registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN" >> ~/.npmrc
      - run: npm version patch
      - run: npm publish
      - run: git push gh-origin HEAD:master --tags
        env:
          NODE_AUTH_TOKEN: ${{secrets.npm_token}}
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

(https://github.com/ether/ep_align/actions/runs/322527776)

I keep getting a 404 error when doing the publish. Which I dont' understand because the package is online here: https://www.npmjs.com/package/ep_align

npm ERR! code E404
npm ERR! 404 Not Found - PUT https://registry.npmjs.org/ep_align - Not found
npm ERR! 404 
npm ERR! 404  '[email protected]' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

This problem is driving me nuts for a few hours now, and I have no idea what it could be. Any ideas?

3

There are 3 answers

0
djb On BEST ANSWER

The NODE_AUTH_TOKEN token is attached to the wrong step, so the npm publish has no authentication. You need:

  - run: npm publish
    env:
      NODE_AUTH_TOKEN: ${{secrets.npm_token}}
  - run: git push gh-origin HEAD:master --tags
      GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

Also, make sure the case of the env variable (npm_token) here matches that in the GitHub actions settings. Environment variables are case senstive.

0
jpjwolli On

Here's what helped me:

  • in .github/workflows/publish.yml make sure to quote the registry-url field

    ...
    - uses: actions/setup-node@v3
            with:
              node-version: 18
              registry-url: 'https://registry.npmjs.org'
    ...
    
  • in .npmrc make sure to include always-auth=true

    //registry.npmjs.org/:_authToken=${NPM_TOKEN}
    registry=https://registry.npmjs.org/
    save-exact=true
    progress=false
    package-lock=true
    always-auth=true
    
3
Canberk Sinangil On

it is just an authentication issue with your token.

Have you ever set the access token in your Repository secrets ? (You might want to create environments as well.)

https://docs.npmjs.com/creating-and-viewing-access-tokens

Or it might be an issue with authorization as well. Please check your token type. It should be automation one.

Here is an example from me > https://github.com/canberksinangil/canberk-playground

Where I have set my token under the repository settings. enter image description here