You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Packages发布npm包失败:请求指向npm官方源而非GitHub源

解决GitHub Actions发布npm包到GitHub Packages时指向npm官方registry的问题

问题分析

从报错日志可以看到,npm publish 请求默认发往了npm官方registry(https://registry.npmjs.org/my-package),而非你指定的GitHub Packages registry(https://npm.pkg.github.com/),导致404错误。以下是针对性的修复方案:


修复步骤

1. 修正Workflow中的publish命令配置

直接在npm publish命令中指定registry,或者通过setup-node动作预设registry:

  • 方式一:命令行指定
    npm publish --registry=https://npm.pkg.github.com/
    
  • 方式二:用actions/setup-node预设(更简洁)
    - uses: actions/setup-node@v4
      with:
        node-version: '20' # 替换为你的Node.js版本
        registry-url: 'https://npm.pkg.github.com/'
    

2. 配置package.json的publish规则

在package.json中添加publishConfig字段,强制发布时使用GitHub Packages:

"publishConfig": {
  "registry": "https://npm.pkg.github.com/"
}

如果是带scope的包(如@你的用户名/包名),可以更精准地绑定scope:

"publishConfig": {
  "@你的用户名:registry": "https://npm.pkg.github.com/"
}

3. 确保Workflow权限正确

在publish任务中声明packages: write权限,并配置认证token:

jobs:
  publish-gpr:
    permissions:
      contents: read
      packages: write
    steps:
      # 其他步骤(checkout、install、build等)
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

4. 清除可能的npm配置冲突

如果之前的npm配置有残留,可在publish前清理默认registry:

npm config delete registry

完整Workflow示例片段

jobs:
  publish-gpr:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          registry-url: 'https://npm.pkg.github.com/'
      - run: npm install
      - run: npm run build
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

内容的提问来源于stack exchange,提问作者toto'

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 17:01:10