registry - 使用 Yarn 从 Github 包注册表安装私有(private)包失败,未经授权

标签 registry yarnpkg github-package-registry npm-registry

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

  • 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 做同样的事情: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 和/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 ),但名称必须全部小写。

    设置
    出版
  • 我创建了一个 private copy这个hello-world存储库。
  • 我复制了上面的.npmrc .yarnrc.yml文件到存储库。
  • 接下来我使用 npm login --registry=https://npm.pkg.github.com/ 登录 yarn npm login --scope=mvce-superstars命令
  • 我输入了我的 github 用户名和我的 token (范围为 read:packagewrite:packagerepo )
  • 最后,我使用 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 (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.
    
    资料来源:
  • 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
  • 关于registry - 使用 Yarn 从 Github 包注册表安装私有(private)包失败,未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61738819/

    相关文章:

    node.js - 类型错误 : fsevents is not a constructor (already tried a bunch of answers)

    node.js - 如何使用带有 typescript 和 out 文件夹的 yarn 工作区?

    javascript - 使用全局 yarn 包获取 VScode 类型

    Gradle 发布到 Github 包注册表返回 403 Forbidden

    nuget - 在 GitHub Packages 上列出 nuget 包

    unit-testing - 以编程方式将.reg文件合并到win32注册表中

    c# - WebRequest.Create 抛出 IOException "The specified registry key does not exist."

    delphi - "Package Cache"注册表项的用途是什么?

    c# - 将可序列化对象保存到 .net 中的注册表?

    github - 从 Github Actions 将 NPM 模块发布到 github 包注册表?