使用Yarn从Github软件包注册表安装私有软件包失败,原因是未授权 [英] Installing private package from Github Package registry using Yarn fails with not authorized
问题描述
此问题与这些相关。但是没有一个解决方案对我有效。
- Yarn can't find private Github npm registry
- Installing private package from Github Package registry fails with not found/not authorized
我可以使用npm install @scope/package
安装软件包而不会出现问题,但是我不能对纱线执行相同的操作:yarn add @scope/package
纱线抛出以下错误:
An unexpected error occurred: "https://npm.pkg.github.com/download/@scope/package/1.2.8/089b08cffb16074c210ec3a59b04de268ae1c7b3a0492dce110adee3ada05bdd: Request failed "401 Unauthorized"".
我的.npmrc文件如下所示:(尝试使用和不使用下面的.ya nrc)
registry=https://registry.npmjs.org/
//npm.pkg.github.com/:_authToken=MY_AUTHTOKEN
@scope:registry=https://npm.pkg.github.com/
我已尝试添加此.ya nrc文件:
registry "https://registry.npmjs.org"
"@scope:registry" "https://npm.pkg.github.com"
(不带.ya nrc)我已尝试此.npmrc文件
registry=https://registry.yarnpkg.com/
@scope:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=MY_AUTHTOKEN
always-auth=true
其中MY_AUTHTOKEN
是我从Github生成的Personal Access Token
。(它可以访问包中的所有内容)
我已尝试:
- 删除纱线.lock
- 删除.ya nrc
- 使用
npm login
使用我的PAT作为密码登录 - 注销NPM并删除全局.npmrc和.ya nrc
- 使用
yarn login
登录
如果有任何念力 我实际尝试的不是@Scope和/Package,而是我的实际作用域和包名。
我确实有权访问Github上的作用域和包。
同样,我的第一个设置与NPM配合得很好。但是我无法将其用于纱线,并且在SO上找不到任何有效的现有解决方案。
推荐答案
以下内容适用于.npmrc
@mvce-superstars:registry=https://npm.pkg.github.com
使用yarn v2时,以下内容在.yarnrc.yml
中对我有效:
npmScopes:
"mvce-superstars":
npmAlwaysAuth: true
npmRegistryServer: "https://npm.pkg.github.com"
首先,请注意小写作用域名称。这应该是发布包的存储库(MVCE-Superstars
)的所有者的名称,但名称必须全部小写。
安装
发布
- 我为此hello-world存储库创建了private copy个存储库。
- 我将上述
.npmrc
或.yarnrc.yml
文件复制到存储库。 - 接下来我使用
npm login --registry=https://npm.pkg.github.com/
或yarn npm login --scope=mvce-superstars
命令登录 - 我输入了我的GitHub用户名和token(作用域
read:package
、write:package
和repo
) - 最后,我使用
npm publish
或yarn npm publish
将包推送到我的私人回购
输出
npm notice
npm notice 📦 @mvce-superstars/hello-world-npm@1.1.1
npm notice === Tarball Contents ===
npm notice 16.3kB example.gif
npm notice 89B bin.js
npm notice 175B lib/index.js
npm notice 734B package.json
npm notice 2.0kB yarn-error.log
npm notice 570B Readme.md
npm notice 167B init.sh
npm notice === Tarball Details ===
npm notice name: @mvce-superstars/hello-world-npm
npm notice version: 1.1.1
npm notice package size: 14.3 kB
npm notice unpacked size: 20.0 kB
npm notice shasum: 5379c8030fa9c5f57e5baef67f2a8a784ce93361
npm notice integrity: sha512-FAI/Wuy4gHW8C[...]FINQeIlZ+HDdg==
npm notice total files: 7
npm notice
+ @mvce-superstars/hello-world-npm@1.1.1
下载
- 我使用
npm init
(use-hello-world-npm
)创建了一个新的NPM项目 - 我将上述
.npmrc
复制到文件夹的根目录 - 下一步我注销NPM(
npm logout --registry=https://npm.pkg.github.com/
),然后重新登录(npm login --registry=https://npm.pkg.github.com/
),确认一下 - 最后,我运行
yarn
,就像它应该运行的那样,它起作用了!
输出
yarn install v1.22.4
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
Done in 0.55s.
纱线v2
➤ YN0000: ┌ Resolution step
➤ YN0014: │ @mvce-superstars/hello-world-npm@npm:^1.1.1: Only some patterns can be imported from legacy lockfiles (not "https://npm.pkg.github.com/download/@mvce-superstars/hello-world-npm/1.1.1/426126f89734c2c76bfac0342c1de9c95ad003b6e905a7b9f9f745892c86da7a#5379c8030fa9c5f57e5baef67f2a8a784ce93361")
➤ YN0000: └ Completed in 0.55s
➤ YN0000: ┌ Fetch step
➤ YN0013: │ @mvce-superstars/hello-world-npm@npm:1.1.1::__archiveUrl=https%3A%2F%2Fnpm.pkg.github.com%2Fdownload%2F%40mvce-superstars%2Fhello-world-npm%2F1.1.1%2F426126f89734c2c76bfac0342c1de9c95ad003b6e905a7b9f9f745892c86da7a can't be found in the cache and will be fetched from the remote server
➤ YN0000: └ Completed in 1.3s
➤ YN0000: ┌ Link step
➤ YN0031: │ One or more node_modules have been detected and will be removed. This operation may take some time.
➤ YN0000: └ Completed
➤ YN0000: Done with warnings in 1.87s
yarn
之后的文件夹内容
.
├── node_modules
│ └── @mvce-superstars
├── package.json
└── yarn.lock
为好起见,我将其删除(yarn remove @mvce-superstars/hello-world-npm
):
yarn remove v1.22.4
[1/2] Removing module @mvce-superstars/hello-world-npm...
[2/2] Regenerating lockfile and installing missing dependencies...
success Uninstalled packages.
Done in 0.06s.
重新添加(yarn add @mvce-superstars/hello-world-npm
):
yarn add v1.22.4
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ @mvce-superstars/hello-world-npm@1.1.1
info All dependencies
└─ @mvce-superstars/hello-world-npm@1.1.1
Done in 1.08s.
来源:
- https://help.github.com/en/packages/using-github-packages-with-your-projects-ecosystem/configuring-npm-for-use-with-github-packages
- https://gemfury.com/help/private-yarn/
- https://github.com/yarnpkg/yarn/issues/4451
这篇关于使用Yarn从Github软件包注册表安装私有软件包失败,原因是未授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!