20

我创建并发布了一个私有的 Github 包。一开始尝试用纱线安装它,我面临以下问题:

无论我尝试使用 yarn 还是 npm,尽管遵循 Github 记录的确切步骤(https://help.github.com/en/github/managing-packages-with-github-package-registry/配置-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 或 - 在我的情况下 - 纱线来实际访问包。现在我面临一个Request failed \"401 Unauthorized\"错误问题,尽管我在上面添加了凭据.yarnrc

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

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

4

4 回答 4

36

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

无论你使用 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需要,至少在使用纱线时(尚未使用 npm 测试)
  • 相反,添加上面的.yarnrc内容是行不通的。当需要身份验证时,纱线会以某种方式出现问题。
  • yarn add @GITHUB_USERNAME/PACKAGE_NAME现在,您可以使用npm 或等效的 npm轻松安装您的私有包。
  • 包括registry=https://registry.yarnpkg.com/纱线或registry=https://registry.npmjs.org/npm

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

于 2019-10-23T12:02:28.517 回答
5

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

我的问题是,虽然在包名称方面npm区分大小写,但在身份验证方面!‍♂️</p> 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.json或您的yarn add命令中匹配此大小写 - 无论您使用的是哪个。例如:

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

我通过挖掘yarn github issues找到了这个解决方案。

于 2020-11-18T16:08:25.693 回答
1

您需要做的是指定检索每个包的位置,在您的 .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 中搜索。无论您将其设置为什么,都将为其他人保留默认注册表。

于 2019-10-31T14:03:18.887 回答
0

我在使用 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
于 2021-12-16T20:10:06.440 回答