我创建并发布了一个私有的 Github 包。起初尝试用yarn安装它,我面临以下问题:
无论我尝试使用yarn还是npm,尽管遵循Github记录的确切步骤(https://help.github.com/en/github/managing-packages-with-github-package-注册表/配置-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 会出现问题。yarn add @GITHUB_USERNAME/PACKAGE_NAME
或等效的 npm 轻松安装您的私有软件包。registry=https://registry.yarnpkg.com/
(对于纱线)或 registry=https://registry.npmjs.org/
(对于 npm)我希望这个解决方案也适用于您的情况。否则,请告诉我您面临哪些问题,我很乐意分享有关该主题的一些研究以及解决方案可能隐藏的位置。
我在这里添加一个答案,因为经过一天在这里和其他地方尝试解决方案的不同变体后,我发现我的问题是别的东西。
我的问题是,虽然
npm
在包名称方面不区分大小写,但在身份验证方面yarn
是! 🤦u200d♂️
因此,使用上面接受的解决方案中的示例:
registry=https://registry.yarnpkg.com/
@GITHUB_USERNAME:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=AUTH_TOKEN
always-auth=true
我需要确保两件事:
@GITHUB_USERNAME
需要 与您在 github 上看到的大小写以及发布包的名称相匹配。也就是说,如果您的用户名是 Pickle-Rick,则需要输入 @Pickle-Rick:registry=https://npm.pkg.github.com
,而不是 @pickle-rick
或 @Pickle-rick
。
您需要在
package.json
或 yarn add
命令中匹配此大小写 - 无论您使用的是哪个。例如:
"@Pickle-Rick/schwifty": "^1.0.0"
中 package.json
或 yarn add @Pickle-Rick/schwifty
。
我通过挖掘
yarn
github问题找到了这个解决方案。
您需要做的是指定在哪里检索每个包,在您的.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 中搜索。无论您将其设置为什么,都会为其他注册表保留默认注册表。
我在 JFrog Artifactory 工作期间遇到了同样的问题...
当我使用
npm install
或 yarn 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
即使您正在运行yarn install,也请使用npmrc 文件。 .npmrc 文件内容:
registry="https://company.pkgs.visualstudio.com/_packaging/design-system/npm/registry/"
always-auth=true
//company.pkgs.visualstudio.com/_packaging/design-system/npm/registry/:username=TheUserName
//company.pkgs.visualstudio.com/_packaging/design-system/npm/registry/:_password=[BASE64_ENCODED_PERSONAL_ACCESS_TOKEN]
//company.pkgs.visualstudio.com/_packaging/design-system/npm/registry/:[email protected]
密码不是来自 Azure DevOps 的 PAT,您首先必须将其转换为 Base64!
node -e "require('readline') .createInterface({input:process.stdin,output:process.stdout,historySize:0}) .question('PAT> ',p => { b64=Buffer.from( p.trim()).toString('base64');console.log(b64);process.exit() })"
;