Node.js `npm发布和npm安装失败取决于所使用的语法 当前行为:
我正在尝试配置一个项目以从NPM安装依赖项。我将以私有包的形式将项目发布到GitHub包。如果我在项目的Node.js `npm发布和npm安装失败取决于所使用的语法 当前行为:,node.js,npm,Node.js,Npm,我正在尝试配置一个项目以从NPM安装依赖项。我将以私有包的形式将项目发布到GitHub包。如果我在项目的.npmrc中使用此语法: @my-org:registry=https://npm.pkg.github.com/ registry=https://npm.pkg.github.com/my-org/ 我可以在本地计算机上使用NPM install从NPM安装依赖项。但是,我无法使用npm publish发布到GitHub包。NPM通知我,我没有经过身份验证。如果我在项目的.npmrc
.npmrc
中使用此语法:
@my-org:registry=https://npm.pkg.github.com/
registry=https://npm.pkg.github.com/my-org/
我可以在本地计算机上使用NPM install
从NPM安装依赖项。但是,我无法使用npm publish
发布到GitHub包。NPM通知我,我没有经过身份验证。如果我在项目的.npmrc
中使用此语法:
@my-org:registry=https://npm.pkg.github.com/
registry=https://npm.pkg.github.com/my-org/
我可以使用npm publish
发布,但无法使用npm install
安装依赖项。NPM告诉我,它正在尝试从GitHub包而不是NPM安装依赖项
预期行为:
根据我的阅读,这两种语法应该与npm install
和npm publish
兼容。然而,根据我的预期用途,我似乎只能使用其中一种
复制步骤:
v15.7.0
和NPM7.4.3
npm登录--scope=@myorg--registry=https://npm.pkg.github.com
~/.npmrc
文件。应改为:
@my-org:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=<auth-token-used-for-login>
.npmrc
添加到我们的项目中:
@my-org:registry=https://npm.pkg.github.com/
npm安装
。安装应该成功
npm发布
。收到以下错误:
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
npm错误!代码ENEEDAUTH
npm错误!需要身份验证此命令要求您登录。
npm错误!需要身份验证您需要使用`npm adduser'授权此计算机`
npm错误!此运行的完整日志可在以下位置找到:
npm错误/Users/my user/.npm/_logs/2021-01-28620_19_55_974Z-debug.log
.npmrc
更改为:
registry=https://npm.pkg.github.com/my-org/
npm发布
。出版应该成功
rm-rf节点_模块/package lock.json
在项目中
npm安装
。收到以下错误:
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
- 操作系统:
- MacOS Catalina 10.15.7
- 马科斯大苏尔11.1
- 节点:15.7.0
- 净现值:7.4.3
publishConfig
不幸的是,
publishConfig
无法解决此问题。它也没有说明两个.npmrc
语法产生不同的结果
项目.npmrc
authToken
NPM的文档说明不需要在项目.npmrc
中包含authToken
。使用npm登录进行身份验证
并将authToken
存储在全局~/.npmrc
中就足够了
变通办法
使用--registry
命令行标志
我已经找到了一个解决方法,直到影响这个问题的NPM错误得到解决。使用以下项目.npmrc
语法时:
@my-org:registry=https://npm.pkg.github.com/
如果我运行
npm publish--registry=https://npm.pkg.github.com/
,我可以成功发布。此外,我可以毫无问题地安装依赖项。我在从1到7的步骤中发现了2个问题
package.json
.npmrc
中,我没有看到//npm.pkg.github.com/:\u authToken=
7.5.3
及以上版本中修复
原始答复(2021年4月20日)
这已被确认为NPM7.x
上的错误。该团队目前正在用他们的软件修复这个bug。在这个bug修复程序发布之前,最好的建议是执行下面列出的解决方法之一
解决方法1:--注册表
命令行标志
运行npm publish
时,添加--registry
标志。此标志允许开发人员指定要发布到的注册表,覆盖.npmrc
或package.json
配置。对于我问题中列出的示例,项目.npmrc
应包含:
@my-org:registry=https://npm.pkg.github.com/
//registry.npmjs.org/:_authToken=dummy
@my-org:registry=https://npm.pkg.github.com/
通过运行以下命令启动时,发布将成功:
npm发布--注册表=https://npm.pkg.github.com/
解决方法2:默认注册表的虚拟令牌
根据,您可以使用默认NPM注册表的虚拟令牌来解决此问题。在我们项目的.npmrc
中,添加以下行:
//registry.npmjs.org/:_authToken=dummy
完整的项目.npmrc
应包含:
@my-org:registry=https://npm.pkg.github.com/
//registry.npmjs.org/:_authToken=dummy
@my-org:registry=https://npm.pkg.github.com/
在这个问题上,我们尝试使用的项目.npmrc
语法都没有指定注册表,因为NPM的文档说明我们不必这样做。文档说明,如果我们的项目.npmrc
中没有,它将检查我们的全局~/.npmrc
。NPM中的错误导致NPM在尝试进行身份验证之前检查项目.npmrc
是否已指定注册表。在:
发生的情况是,当前cli仅在查看您是否已登录时查找您配置的“注册表”设置。因此,临时解决方案是覆盖该设置(就像您通过传递--registry所做的那样),或者为默认(npm)注册表添加一个令牌,以便令牌的检查不会失败。该检查只是查找配置中是否存在令牌,而不是对其进行验证(如果在实际请求期间使用令牌,则会发生这种情况),因此在配置中输入一个伪值将停止错误,直到该PR到达
添加虚拟令牌后,运行
npm publish
和npm install
应该都会成功。不幸的是,publishConfig
无法解决此问题。它也没有说明两个.npmrc
语法产生不同的结果。此外,NPM的文档说明不需要在项目.npmrc
中包含authToken
。认证