我刚刚在 GitHub 上发布了一个私有(private)包,试图弄清楚它应该如何工作。现在我正在尝试将它安装在另一个项目中。我通过 npm login --registry=https://npm.pkg.github.com
进行了身份验证使用具有 write:packages
的访问 token , read:packages
和 repo
特权。在尝试运行 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 配置中:
https://github.com/settings/tokens
(设置 > 开发人员设置 > 个人访问 token )您应该会看到如下内容:Generate new token
read:packages
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/