从 Github 包注册表安装私有包失败,未获得 found/not 授权

Installing private package from Github Package registry fails with not found/not authorized

我创建并发布了一个私人 Github 包。一开始尝试用yarn安装,遇到如下问题:

无论我尝试使用 yarn 还是 npm,尽管按照 Github (https://help.github.com/en/github/managing-packages-with-github-package-registry/configuring-npm-for-use-with-github-package-registry).

记录的确切步骤进行操作,但它根本找不到包

我的.yarnrc:

registry "https://npm.pkg.github.com/OWNER"

对于 yarn,它不断尝试在 https://registry.yarnpkg.com/@GITHUB_USERNAME 中查找包,而不是我在上面输入的注册表。

备注:在 .yarnrc 注册表中需要添加一个稍微不同的语法:

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

到目前为止,我也开始尝试混合使用 .npmrc.yarnrc 配置,但没有成功。

-

编辑(部分解决)

我想出了如何使用 npm 或 - 在我的例子中 - yarn 实际访问包。现在我面临 Request failed \"401 Unauthorized\" 错误的问题,尽管我在 .yarnrc:

之上添加了凭据

//npm.pkg.github.com/:_authToken=AUTH_TOKEN

.npmrc 中做同样的事情也不起作用。

我找到了解决方案,不幸的是,它在任何地方都没有很好的记录,而是混合了不同的资源 - 而且它非常简单。

不管你是用 npm 还是 yarn,只要有下面的 .npmrc(yarn 也会包含这个):

registry=https://registry.yarnpkg.com/

@GITHUB_USERNAME:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=AUTH_TOKEN
always-auth=true

一些评论:

  • always-auth 是必需的,至少在使用 yarn 时是这样(尚未使用 npm 进行测试)
  • .yarnrc 中添加以上内容无效。当需要身份验证时,纱线会以某种方式出现问题。
  • 现在您可以使用 yarn add @GITHUB_USERNAME/PACKAGE_NAME 或 npm 等价物轻松安装您的私人包。
  • 包括 registry=https://registry.yarnpkg.com/ 用于 yarn 或 registry=https://registry.npmjs.org/ 用于 npm

我希望这个解决方案也适用于您的情况。否则请告诉我您面临的问题,我很乐意分享关于该主题的一些研究以及解决方案可能隐藏的地方。

您需要做的是指定在何处检索每个包,在您的 .npmrc 中使用类似的内容(我不知道 yarn 语法,但它在读取 .npmrc 时与 yarn 一起使用):

//registry.npmjs.org/:_authToken=<token-npm-read>
//npm.pkg.github.com/:_authToken=<token-github-package-read>
@foo:registry=https://npm.pkg.github.com
@far:registry=https://registry.npmjs.org

然后,Yarn 会在Github 中搜索@foo/mypackage1,其中@far/mypackage2 会在npmjs 中搜索。默认注册表将为其他人保留,无论您将其设置为什么。

我在这里添加一个答案,因为在这里和其他地方尝试了不同的解决方案一天后,我发现我的问题是另外一回事。

我的问题是,虽然 npm 对包名称不区分大小写,但 yarn 说到身份验证! ‍♂️

因此,使用上面 中的示例:

registry=https://registry.yarnpkg.com/

@GITHUB_USERNAME:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=AUTH_TOKEN
always-auth=true

我需要确保两件事:

  1. @GITHUB_USERNAME 需要 来匹配您在 github 上看到的情况并且发布包的名称。也就是说,如果您的用户名是 Pickle-Rick,您需要输入 @Pickle-Rick:registry=https://npm.pkg.github.com,而不是 @pickle-rick@Pickle-rick.

  2. 您需要在 package.jsonyarn add 命令中匹配此大小写 - 无论您使用哪个。例如:

    "@Pickle-Rick/schwifty": "^1.0.0"package.jsonyarn add @Pickle-Rick/schwifty.

我通过挖掘 yarn github issues.

找到了这个解决方案

我在使用 JFrog Artifactory 时遇到了同样的问题...

当我使用 npm installyarn install 时,我总是遇到错误:

 => ERROR [builder 4/6] RUN yarn install                                                                                                         61.0s
> [builder 4/6] RUN yarn install:
#12 0.358 yarn install v1.22.15
#12 0.474 [1/4] Resolving packages...
#12 1.199 [2/4] Fetching packages...
#12 6.330 error An unexpected error occurred: 
"https://customer.jfrog.io/dxg/api/npm/npm-local/@xx/components-react16/-/@xx/components-react16-1.2.0.tgz: 
Request failed \"401 Unauthorized\"".

我使用生成 authToken 和文件 .npmrc(user_home 文件夹)和命令:

npm login --registry=https://dxg.jfrog.net/dxg/api/npm/npm-local/ --scope=@xx

解决方案:

当我们可以直接从 Artefactory 获取 .npmrc(或 yarnrc) 文件所需的所有配置时存在方法:

curl -u<USER_NAME>:<AUTH_TOKEN> "https://<your_domain>/artifactory/api/npm/npm-local/auth/jfrog"

从响应中获取数据后,您应该手动将其放入 .npmrc 文件。

例如响应的近似结构:

@xx:registry=https://<your_domain>/artifactory/api/npm/npm-local/
//<your_domain>/dxg/api/npm/npm-local/:_authToken=<auth_Token>
//<your_domain>/artifactory/api/npm/npm-local/:_password=<password>
//<your_domain>/artifactory/api/npm/npm-local/:username=<user_name>
//<your_domain>/artifactory/api/npm/npm-local/:email=<email>
//<your_domain>/artifactory/api/npm/npm-local/:always-auth=true