`npm publish` 和 `npm install` 失败取决于使用的 `.npmrc` 语法

Jor*_*est 6 node.js npm

当前行为:

我正在尝试配置一个项目以从 NPM 安装依赖项。我将把项目作为私有包发布到 GitHub Packages。如果我在我的项目中使用此语法.npmrc

@my-org:registry=https://npm.pkg.github.com/
Run Code Online (Sandbox Code Playgroud)

我可以npm install在本地机器上使用 NPM 安装依赖项。但是,我无法使用npm publish. NPM 通知我我没有通过身份验证。如果我在我的项目中使用此语法.npmrc

registry=https://npm.pkg.github.com/my-org/
Run Code Online (Sandbox Code Playgroud)

我可以使用 发布npm publish,但无法使用npm install. NPM 通知我它正在尝试从 GitHub 包而不是 NPM 安装依赖项。

预期行为:

根据我的阅读,这两种语法都应该与npm install和兼容npm publish。但是,根据我的预期用途,我似乎只能使用其中一种。

重现步骤:

  1. 通过nvm安装 Nodev15.7.0和 NPM 。7.4.3

  2. 使用以下命令登录 GitHub Packages:

    @my-org:registry=https://npm.pkg.github.com/
    
    Run Code Online (Sandbox Code Playgroud)
  3. 检查我们~/.npmrc的主文件夹中的文件。它应该是:

    @my-org:registry=https://npm.pkg.github.com/
    //npm.pkg.github.com/:_authToken=<auth-token-used-for-login>
    
    Run Code Online (Sandbox Code Playgroud)
  4. 使用以下内容创建项目package.json

    registry=https://npm.pkg.github.com/my-org/
    
    Run Code Online (Sandbox Code Playgroud)
  5. 将以下内容添加.npmrc到我们的项目中:

    @my-org:registry=https://npm.pkg.github.com/
    
    Run Code Online (Sandbox Code Playgroud)
  6. 运行npm install。安装应该成功。

  7. 运行npm publish。收到以下错误:

    npm login --scope=@my-org --registry=https://npm.pkg.github.com
    
    Run Code Online (Sandbox Code Playgroud)
  8. 将项目更改.npmrc为:

    registry=https://npm.pkg.github.com/my-org/
    
    Run Code Online (Sandbox Code Playgroud)
  9. 运行npm publish。发布应该成功。

  10. rm -rf node_modules/ package-lock.json 在项目中。

  11. 运行npm install。收到以下错误:

    npm ERR! code E401
    npm ERR! Incorrect or missing password.
    npm ERR! If you were trying to login, change your password, create an
    npm ERR! authentication token or enable two-factor authentication then
    npm ERR! that means you likely typed your password in incorrectly.
    npm ERR! Please try again, or recover your password at:
    npm ERR!     https://www.npmjs.com/forgot
    npm ERR!
    npm ERR! If you were doing some other operation then your saved credentials are
    npm ERR! probably out of date. To correct this please try logging in again with:
    npm ERR!     npm login
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     /Users/my-user/.npm/_logs/2021-01-28T20_38_20_711Z-debug.log
    
    Run Code Online (Sandbox Code Playgroud)

环境:

  • 操作系统:
    • MacOS Catalina 10.15.7
    • MacOS 大苏尔 11.1
  • 节点:15.7.0
  • npm:7.4.3

我尝试过的事情

使用 publishConfig

不幸的是,publishConfig不能解决问题。它也没有解决两种.npmrc语法产生不同结果的问题。

项目 .npmrc authToken

NPM 的文档说明不需要authToken在项目中包含.npmrc. 进行身份验证npm login并将其存储authToken在全局中~/.npmrc就足够了。

解决方法

使用--registry命令行标志

在解决影响此问题的 NPM 错误之前,我找到了一种解决方法。使用以下项目.npmrc语法时:

@my-org:registry=https://npm.pkg.github.com/
Run Code Online (Sandbox Code Playgroud)

如果我运行npm publish --registry=https://npm.pkg.github.com/,我可以成功发布。此外,我可以毫无问题地安装依赖项。

Jor*_*est 14

更新(2021 年 5 月 30 日)

这已在 NPM7.5.3及更高版本中修复。

原答案(2021年4月20日)

这已被确认为 NPM 上的错误7.x。该团队目前正在通过GitHub Pull Request #2602修复该错误。在此错误修复发布之前,最好的建议是执行下面列出的解决方法之一。

解决方法 1:--registry命令行标志

运行时npm publish,添加--registry标志。该标志使开发人员能够指定他们想要发布到、覆盖.npmrcpackage.json配置的注册表。对于我的问题中列出的示例,该项目.npmrc应包含:

@my-org:registry=https://npm.pkg.github.com/
Run Code Online (Sandbox Code Playgroud)

通过运行以下命令启动发布将成功:

@my-org:registry=https://npm.pkg.github.com/
Run Code Online (Sandbox Code Playgroud)

解决方法 2:默认注册表的虚拟令牌

根据npm 开发人员 wraithgar 对此 bug 的 GitHub 问题的评论,您可以使用默认 NPM 注册表的虚拟令牌来解决此问题。在我们的项目中.npmrc,添加以下行:

//registry.npmjs.org/:_authToken=dummy
Run Code Online (Sandbox Code Playgroud)

完整的项目.npmrc应包含:

//registry.npmjs.org/:_authToken=dummy
@my-org:registry=https://npm.pkg.github.com/
Run Code Online (Sandbox Code Playgroud)

在这个问题中,我们尝试使用的项目.npmrc语法都没有指定注册表,因为 NPM 的文档表明我们不必这样做。文档指出,~/.npmrc如果我们的项目中没有全局变量,它将检查我们的全局变量.npmrc。NPM 中的错误导致 NPM.npmrc在尝试进行身份验证之前检查项目是否已指定注册表。用幽灵加自己的话说

发生的情况是,当前 cli 仅在查看您是否已登录时查找您配置的“注册表”设置。因此临时解决方案是覆盖该设置(就像您通过传递 --registry 所做的那样),或者添加默认 (npm) 注册表的令牌,以便令牌检查不会失败。检查只是在配置中查找令牌的存在,而不是验证它(当然,如果在实际请求期间使用它,就会发生这种情况),因此放入一个虚拟值将停止错误,直到 PR土地。

添加虚拟令牌后,运行npm publishnpm install应该都会成功。


Art*_* S. 10

我有同样的错误。我修复的第一件事是 npm 版本 > 7.5.3,如已接受的答案中所建议的,但没有运气,同样的错误:

npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in.
npm ERR! need auth You need to authorize this machine using `npm adduser`
Run Code Online (Sandbox Code Playgroud)

在尝试了所有建议的解决方法后,它仍然给出相同的输出。最后我找到了原因:我忘记在package.json“name”属性中添加范围,所以通过更改解决了问题

{
  "name": "my-package",
  ...
Run Code Online (Sandbox Code Playgroud)

{
  "name": "@my-org/my-package",
  ...
Run Code Online (Sandbox Code Playgroud)