7

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  'ep_align@0.2.7' 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?

KushalSeth
  • 3,265
  • 1
  • 26
  • 29
Enrico
  • 2,734
  • 1
  • 27
  • 40

3 Answers3

6

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

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • 1
    Why does it say "ep_align@0.2.7' is not in the npm registry" if it's an authentication issue? The package is public. – Enrico Feb 04 '22 at 09:34
  • Can you publish a package without authentication? If yes, "you could publish the packages to any npm registry". So of course you need a token. – Canberk Sinangil Jul 20 '22 at 09:21
  • 2
    It could have given you a 401 instead of 404 when you are not authenticated for the right permission. However, NPM tries to protect private packages by not announcing whether they exist or not. As a result, you get a 404 instead of 401. The package doesn't exist. Don't know if it's just you being unauthorized. – Cat Chen Oct 17 '22 at 05:35
1

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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
djb
  • 4,930
  • 1
  • 34
  • 37
0

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
    
desertnaut
  • 57,590
  • 26
  • 140
  • 166
jpjwolli
  • 101
  • 2