使用 Yarn 从 Github 包注册表安装私有包失败,未授权

Installing private package from Github Package registry using Yarn fails with not authorized

这个问题与这些有关。但是 none 的解决方案对我有用。

我可以用 npm install @scope/package 安装一个没有问题的包,但是我不能用 yarn 做同样的事情:yarn add @scope/package

yarn 抛出以下错误: An unexpected error occurred: "https://npm.pkg.github.com/download/@scope/package/1.2.8/089b08cffb16074c210ec3a59b04de268ae1c7b3a0492dce110adee3ada05bdd: Request failed \"401 Unauthorized\"".

我的 .npmrc 文件看起来像这样:(在 .yarnrc 下尝试使用和不使用)

registry=https://registry.npmjs.org/
//npm.pkg.github.com/:_authToken=MY_AUTHTOKEN
@scope:registry=https://npm.pkg.github.com/

我试过添加这个 .yarnrc 文件:

registry "https://registry.npmjs.org"
"@scope:registry" "https://npm.pkg.github.com"

(没有 .yarnrc)我试过这个 .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。 (它可以访问包中的所有内容)

我尝试过:

如有任何混淆 我实际上不是在尝试@scope 和 /package,而是我的实际范围和包名称。

我确实可以访问 Github 上的范围和包。

再一次,我的第一个设置在 npm 上运行得很好。但是我无法使用 yarn 进行此操作,也无法在 SO 上找到任何有效的现有解决方案。

您只需要在项目的根目录中使用 .npmrc 并包含以下内容:

//npm.pkg.github.com/:_authToken=GITHUB_PERSONAL_TOKEN
@OWNER:registry=https://npm.pkg.github.com

请记住,GITHUB_PERSONAL_TOKEN 需要 read:packages 范围权限才能从您的私人仓库中读取包.

以下对我有用 .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) 所有者的名称,但名称必须全部小写。


设置

发布

  • 我创建了一个 private copy of this hello-world 存储库。
  • 我复制了上面的 .npmrc .yarnrc.yml 文件到存储库中。
  • 接下来我使用 npm login --registry=https://npm.pkg.github.com/ OR yarn npm login --scope=mvce-superstars 命令登录
  • 我输入了我的 github 用户名和 token(范围为 read:packagewrite:packagerepo
  • 最后,我使用 npm publish OR 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.

来源:

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

我的问题是,虽然 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.

找到了这个解决方案