17

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

我可以毫无问题地安装一个包,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 文件看起来像这样:(在 .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

我从 Github 生成的我在MY_AUTHTOKEN哪里。Personal Access Token(它可以访问包中的所有内容)

我试图:

  • 删除 yarn.lock
  • 删除 .yarnrc
  • npm login使用我的 PAT 作为密码登录
  • 注销 npm 并删除全局 .npmrc 和 .yarnrc
  • 登录yarn login

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

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

再次,我的第一个设置适用于 npm。但是我无法使用纱线来解决这个问题,并且在 SO 上找不到任何有效的现有解决方案。

4

3 回答 3

14

以下对我有用.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存储库的私有副本
  • 我将上述.npmrc OR .yarnrc.yml文件复制到存储库中。
  • 接下来我使用npm login --registry=https://npm.pkg.github.com/ OR yarn npm login --scope=mvce-superstars命令登录
  • 我输入了我的 github 用户名和我的令牌(带有 scopes read:packagewrite:packagerepo
  • 最后,我使用OR将包推送到我的私人仓库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.

资料来源:

于 2020-05-27T08:51:08.367 回答
5

您只需要在项目的根目录中使用.npmrc 即可:

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

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

于 2020-05-26T11:22:38.733 回答
1

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

我的问题是,虽然在包名称方面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:09:35.080 回答