当前行为:
我正在尝试配置项目以安装来自 NPM 的依赖项。我将把项目作为私有包发布到 GitHub Packages。如果我在我的项目中使用这个语法.npmrc
:
@my-org:registry=https://npm.pkg.github.com/
我可以使用 NPM 安装依赖项npm install
在我的本地机器上。但是,我无法使用以下方式发布到 GitHub Packagesnpm publish
。 NPM 通知我我未经过身份验证。如果我在我的项目中使用这个语法.npmrc
:
registry=https://npm.pkg.github.com/my-org/
我可以使用发布npm publish
,但我无法安装依赖项npm install
。 NPM 通知我它正在尝试从 GitHub Packages 而不是 NPM 安装依赖项。
预期行为:
根据我的阅读,两种语法都应该兼容npm install
and npm publish
。但是,根据我的预期用途,我似乎只能使用其中之一。
重现步骤:
-
安装节点v15.7.0
和国家公共管理7.4.3
via nvm https://github.com/nvm-sh/nvm.
-
使用以下命令登录 GitHub Packages:
npm login --scope=@my-org --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>
-
使用以下内容创建项目package.json
:
{
"name": "@my-org/my-package",
"description": "A test.",
"version": "1.0.0",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/my-org/my-package.git"
},
"keywords": ["example"],
"author": "Me",
"license": "ISC",
"bugs": {
"url": "https://github.com/my-org/my-package/issues"
},
"homepage": "https://github.com/my-org/my-package",
"dependencies": {
"bootstrap": "^4.5.2"
}
}
-
添加以下内容.npmrc
到我们的项目:
@my-org:registry=https://npm.pkg.github.com/
-
Run npm install
。安装应该成功。
-
Run npm publish
。收到以下错误:
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`
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/my-user/.npm/_logs/2021-01-28T20_19_55_974Z-debug.log
-
改变项目.npmrc
to:
registry=https://npm.pkg.github.com/my-org/
-
Run npm publish
。发布应该会成功。
-
rm -rf node_modules/ package-lock.json
在项目中。
-
Run 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
环境:
- OS:
- MacOS 卡塔琳娜 10.15.7
- MacOS 大苏尔 11.1
- 节点:15.7.0
- npm:7.4.3
我尝试过的事情
Using publishConfig
很遗憾,publishConfig
并不能解决问题。它也没有解决这两个问题.npmrc
语法产生不同的结果。
Project .npmrc
authToken
NPM 的文档指出不需要包含authToken
在项目中.npmrc
。验证方式为npm login
并存储authToken
在全球范围内~/.npmrc
应该足够了。
解决方法
使用--registry
命令行标志
我找到了解决方法,直到影响此问题的 NPM 错误得到解决。使用以下项目时.npmrc
syntax:
@my-org:registry=https://npm.pkg.github.com/
如果我跑npm publish --registry=https://npm.pkg.github.com/
,就可以发布成功了。此外,我可以毫无问题地安装依赖项。