Github Actions:NPM发布404未找到

7

在我的Github项目中,我正在尝试在将某些内容推送到主分支时自动创建新版本并将其发布到NPM。

思路

  1. 创建一个新的次要版本
  2. 将软件包发布到NPM

我正在使用GitHub Actions。我的工作流文件如下:

# 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)

我在发布时一直遇到404错误,但我不明白为什么,因为这个包在线上是存在的: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.

这个问题已经困扰我几个小时了,但我不知道它可能是什么。 有任何想法吗?


https://dev59.com/UFkT5IYBdhLWcg3wELgl - quine9997
3个回答

6

1
如果这是一个身份验证问题,为什么会显示“ep_align@0.2.7”不在npm注册表中?该软件包是公共的。 - Enrico
你能在不进行身份验证的情况下发布一个包吗?如果可以,“你就可以将包发布到任何npm注册表中”。因此,你当然需要一个令牌。 - Canberk Sinangil
2
当您未被授权访问正确的权限时,它可能会给您返回401而不是404。然而,NPM试图通过不公布私有包是否存在来保护它们。因此,您会收到404而不是401。该软件包不存在。不知道是否只是您未经授权。 - Cat Chen

1

NODE_AUTH_TOKEN令牌附加到错误的步骤,因此npm publish没有身份验证。您需要:

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

此外,请确保此处环境变量(npm_token)的大小写与 GitHub actions 设置中的匹配。环境变量是区分大小写的。

0

以下是帮助我的方法:

  • .github/workflows/publish.yml中,请确保引用registry-url字段

    ...
    - uses: actions/setup-node@v3
            with:
              node-version: 18
              registry-url: 'https://registry.npmjs.org'
    ...
    
  • .npmrc中,请确保包含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
    

  • 网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接