github - 从 github npm 注册表安装包 - 身份验证错误 401

标签 github npm npm-install npm-login

我刚刚在 GitHub 上发布了一个私有(private)包,试图弄清楚它应该如何工作。现在我正在尝试将它安装在另一个项目中。我通过 npm login --registry=https://npm.pkg.github.com 进行了身份验证使用具有 write:packages 的访问 token , read:packagesrepo特权。在尝试运行 npm install https://npm.pkg.github.com/@orgname/package-name 时我收到一条错误消息:

npm ERR! code E401
npm ERR! Unable to authenticate, need: Basic realm="GitHub Package Registry"

如何添加/获取此特权?

最佳答案

除了在 npm 配置中设置注册表之外,您还需要在 Github 上生成个人访问 token 并将其添加到您的 npm 配置中:

  • 在 Github 中导航到 https://github.com/settings/tokens (设置 > 开发人员设置 > 个人访问 token )您应该会看到如下内容:

  • enter image description here
  • 点击Generate new token
  • 从权限中至少选择 read:packages

  • enter image description here
  • 点击Generate token并复制 token
  • 将以下内容添加到您的本地 .npmrc :
    @${OWNER}:registry=https://npm.pkg.github.com
    //npm.pkg.github.com/:_authToken=${TOKEN}
    

  • 见相关Github Packages documentation

    Related: For Github Actions, be aware of the difference between the GITHUB_TOKEN and a personal access token. The Github Token's permissions are limited to the repository that contains your workflow. For anything else (including granular permissions beyond those allowed for the Github Token) you need a personal access token.

    关于github - 从 github npm 注册表安装包 - 身份验证错误 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58919401/

    相关文章:

    github - 如何访问 Github for Enterprise 上的 raw.githubusercontent.com?

    git - 开源项目时处理以前的提交

    javascript - Polymer3 中可以使用哪个表格元素?

    node.js - NPM:错误:已知 npm 不能在 Node.js v10.19.0 上运行

    node.js - NPM 安装错误

    javascript - 如何为 JS 库上传和创建 @type/myLibrary

    git - 如何将提交分成多个 squashes

    git - 如何跳过推送事件的 GitHub Actions 作业?

    node.js - 尝试在 Windows 上使用 npm 安装 yo

    node.js - NPM:npm 安装在 fetchMetaData -> addTmpTa 上挂起