使用Yarn从Github包注册表安装私有包时,出现未授权的情况。

问题描述 投票:1回答:1

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

我可以安装一个软件包,而不会出现以下问题 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 是我 Personal Access Token 我已经从Github生成。(它可以访问包中的所有内容)

我试过了

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

为了防止任何混淆,我实际上并没有尝试@scope和包,而是我的实际范围和包名。

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

而且我的第一个设置在npm下也能正常工作。但我无法让这个与yarn一起工作,也无法在SO上找到任何有效的现有解决方案。

registry yarn yarnpkg github-package-registry npm-registry
1个回答
1
投票

下面的方法对我有用 .npmrc

@mvce-superstars:registry=https://npm.pkg.github.com

使用 纱线v2以下是对我有用的方法 .yarnrc.yml:

npmScopes:
  "mvce-superstars":
    npmAlwaysAuth: true
    npmRegistryServer: "https://npm.pkg.github.com"

首先,注意到 小写 scope名称。这应该是版本库所有者的名字(MVCE-Superstars)的地方,但名称必须是小写的。


设置的

出版

  • 我创建了一个 抄袭你好世界 仓库。
  • 我复制了上面的 .npmrc .yarnrc.yml 文件到repoository。
  • 接下来,我使用 npm login --registry=https://npm.pkg.github.com/ yarn npm login --scope=mvce-superstars 指挥
  • 我输入了我的github用户名和我的 象征性 (带范围 read:package, write:packagerepo)
  • 最后,我把这个包用 npm publish yarn npm publish

产量

npm notice 
npm notice 📦  @mvce-superstars/[email protected]
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/[email protected]

下载

  • 我使用以下方法创建了一个新的npm项目 npm init (use-hello-world-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/[email protected]
info All dependencies
└─ @mvce-superstars/[email protected]
Done in 1.08s.

储存库位于 此处所以,如果你想亲眼看看它的工作方式,就像我解释的那样,请在下面用你的用户名在github上评论,我将向你发出邀请。

资料来源


0
投票

您只需要使用 .npmrc 在你的项目根目录下添加这些内容。

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

请记住 GITHUB_PERSONAL_TOKEN(中文) 需要 read:packages 范围权限,以便从你的私有仓库读取软件包。

© www.soinside.com 2019 - 2024. All rights reserved.