Github 操作:NPM 发布 404 未找到

Enr*_*ico 7 continuous-integration npm github-actions building-github-actions

在我的 github 项目中,每当有东西被推送到 master 分支时,我都会尝试自动创建一个新版本并将其发布到 NPM。

这个想法

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

我正在使用 github 操作。我的工作流程文件如下所示:

# 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}}
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

这个问题让我发疯了几个小时,我不知道它是什么。有任何想法吗?

小智 6

这只是您的令牌的身份验证问题。

您是否曾在您的 中设置过访问令牌Repository secrets?(您可能还想创建环境。)

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

或者也可能是授权问题。请检查您的令牌类型。应该是自动化之一。

这是我的一个例子> https://github.com/canberksinangil/canberk-playground

我在存储库设置下设置了我的令牌。 在此输入图像描述

  • 当您未通过正确权限的身份验证时,它可能会给您 401 而不是 404。然而,NPM 试图通过不宣布它们是否存在来保护私有包。结果,您得到 404 而不是 401。该包不存在。不知道是不是只有你没有权限。 (2认同)

djb*_*djb 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}}
Run Code Online (Sandbox Code Playgroud)

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