未找到 - PUT https://npm.pkg.github.com/package-name

27

我正在尝试上传一个包到GPR(Github包注册表)。我已经成功登录:

npm login --registry=https://npm.pkg.github.com

然后运行以下命令:

npm set registry https://npm.pkg.github.com/

npm publish

返回此错误:

npm ERR! 404 Not Found - PUT https://npm.pkg.github.com/package-name
npm ERR! 404 
npm ERR! 404  'package-name@version' is not in the npm registry.

看起来它试图将软件包上传到npm注册表,而不是GitHub软件包注册表。我该如何解决这个问题?

->

似乎它试图在npm注册表上上传一个软件包,而不是GitHub软件包注册表。我该怎么解决这个问题?


1
你的 package.json 文件长成什么样子?你在里面指定了 publishConfig.registry 吗?值得注意的是,在命令行上指定 registry 并不能覆盖它。 - Edward Thomson
不,我根本没有在package.json中指定“publishConfig”字段。 - kaxi1993
5个回答

32

解决此问题的两种方法:

  1. package.json中指定publishConfig选项:
"publishConfig": {
    "registry":"https://npm.pkg.github.com/@OWNER"
},
  1. 在您的项目中添加.npmrc文件,并使用以下内容:
registry=https://npm.pkg.github.com/@OWNER

请将OWNER替换为您在GitHub上拥有存储库并发布软件包的用户或组织账户的名称。


4
问题已经解决了,但是我又遇到了一个新的问题。405方法不允许 - yussan
是的,在 package.json 中添加 repository 属性,指向您在 GitHub 上的仓库。 - yussan
3
@yussan 你是如何解决 405 Method Not Allowed 的问题的? - Serene Abraham Mathew
1
@yussan 我的意思是你是如何解决它的? - Serene Abraham Mathew
1
我有同样的问题。 - Prathamesh More
显示剩余2条评论

19
例如:
{
  "name": "@elvisjs/calling-elvis",
  "repository": {
    "type": "git",
    "url": "https://github.com/elvisjs/calling-elvis"
  },
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/elvisjs"
  }
}

namerepository/urlpublishConfig/registry 必须匹配。


1
非常感谢,我一直在尝试理解为什么我不能发布好几个小时!太奇怪了,我为什么需要有一个存储库块才能发布???甚至没有得到一个好的错误消息和清晰的文档描述。这真是疯狂... - Max
你的解决方案对我也有效。但是我需要补充一点,不要像我一样混淆连字符和下划线 :) - Atakan Savaş

13

Github软件包注册表期望在package.json中的name属性为"@{github用户名}/{软件包名称}" 例如:-

"name": "@pravanjan/local-time",
"publishConfig": { 
     "registry": "https://npm.pkg.github.com/" 
 },
如果package.json中未设置"publishConfig",我们可以直接在终端中设置registry参数。
npm publish --registry=https://npm.pkg.github.com/ 

这对我起作用了


2
正如其他回答所暗示的那样,上述错误的根本原因是GPR(与https://www.npmjs.com/不同)要求包具有作用域。然而,似乎所有其他建议的解决方案(更新package.json等)都不能在不使用作用域的情况下继续将软件包发布到https://www.npmjs.com/。以下是我允许两者的解决方案:
假设:
1. package.json中包含简单的包名称,没有作用域,通常在将公共包发布到https://www.npmjs.com/时会出现这种情况 2. GitHub工作流程是使用GitHub的Node.js Package模板配置的
publish-gpr作业中添加一个额外的步骤,在默认步骤run: npm ci之前动态地将当前存储库的所有者插入到package.json中的包名称中:
- name: Insert repository owner as scope into package name
  run: |
    node <<EOF
    const fs = require('fs').promises;
    fs.readFile('package.json', 'utf8').then((data) => JSON.parse(data)).then((json) => {
        json.name = '@$(echo "$GITHUB_REPOSITORY" | sed 's/\/.\+//')/' + json.name;
        console.info('Package name changed to %s', json.name);
        return fs.writeFile('package.json', JSON.stringify(json), 'utf8');
    }).catch(error => {
        console.error(error);
        process.exit(1);
    });
    EOF

0

选项1)将代码发布到Github包注册表https://npm.pkg.github.com/),需要通过Github工作流正确配置3个文件。

步骤1:从包根目录中,在./github/workflows/release-package.yml中创建文件。确保registry-url为https://npm.pkg.github.com/。请参考https://github.com/vernGlobe/json-light-query/blob/main/.github/workflows/release-package.yml

步骤2:在“package.json”中进行配置:

"name": "@<username>/json-light-query",
"publishConfig": {
   "@<username>:registry":"https://npm.pkg.github.com/"
 }

步骤3:将以下内容添加到“.npmrc”文件中:

@<username>:registry=https://npm.pkg.github.com

注意:请将<username>替换为您的GitHub帐户用户名。

选项2:发布到npm软件包注册表https://registry.npmjs.org/)。只需要正确配置package.json文件即可。

"name": "json-light-query",
"publishConfig": {
    "@<username>:registry": "https://registry.npmjs.org/"
}

注意:请将<username>替换为您的npmjs帐户用户名。
https://www.npmjs.com/下载/搜索软件包。

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