这个问题与这些有关。但是没有一个解决方案对我有用。
我可以毫无问题地安装软件包
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 生成的。 (它可以访问包中的所有内容)我试图:
npm login
登录使用我的 PAT 作为密码 yarn login
登录如有任何混淆
我实际上并没有尝试 @scope 和/package 而是我的实际范围和包名称。
我确实可以访问 Github 上的范围和包。
再次,我的第一个设置适用于 npm。但是我无法使用 yarn 来解决这个问题,并且在 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
),但名称必须全部小写。设置
出版
.npmrc
或 .yarnrc.yml
文件到存储库。 npm login --registry=https://npm.pkg.github.com/
登录或 yarn npm login --scope=mvce-superstars
命令 read:package
、 write:package
和 repo
)npm publish
将包推送到我的私有(private)仓库。 或 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
创建了一个新的 npm 项目( use-hello-world-npm
) .npmrc
到文件夹的根目录 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.
yarn 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.
资料来源:关于registry - 使用 Yarn 从 Github 包注册表安装私有(private)包失败,未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61738819/