[英]`npm publish` and `npm install` fail depending on `.npmrc` syntax used
我正在嘗試配置一個項目以從 NPM 安裝依賴項。 我會將項目作為私有 package 發布到 GitHub 包。 如果我在項目的.npmrc
中使用此語法:
@my-org:registry=https://npm.pkg.github.com/
我可以使用npm install
在我的本地計算機上安裝 NPM 的依賴項。 但是,我無法使用npm publish
發布到 GitHub 包。 NPM 通知我我未通過身份驗證。 如果我在項目的.npmrc
中使用此語法:
registry=https://npm.pkg.github.com/my-org/
我可以使用npm publish
,但我無法使用npm install
安裝依賴項。 NPM 通知我它正在嘗試從 GitHub 包安裝依賴項,而不是 NPM。
根據我的閱讀,這兩種語法都應該與npm install
和npm publish
兼容。 但是,根據我的預期用途,我似乎只能使用其中一種。
通過nvm安裝 Node v15.7.0
和 NPM 7.4.3
。
使用以下命令登錄 GitHub 包:
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/
運行npm install
。 安裝應該成功。
運行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
更改為:
registry=https://npm.pkg.github.com/my-org/
運行npm publish
。 發布應該成功。
rm -rf node_modules/ package-lock.json
在項目中。
運行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
publishConfig
不幸的是, publishConfig
沒有解決這個問題。 它也沒有解決兩種.npmrc
語法產生不同結果的問題。
.npmrc
authToken
NPM 的文檔指出不需要在項目.npmrc
中包含authToken
。 使用npm login
進行身份驗證並將authToken
存儲在全局~/.npmrc
中就足夠了。
--registry
命令行標志在解決影響此問題的 NPM 錯誤之前,我找到了一種解決方法。 使用以下項目.npmrc
語法時:
@my-org:registry=https://npm.pkg.github.com/
如果我運行npm publish --registry=https://npm.pkg.github.com/
,我可以成功發布。 此外,我可以毫無問題地安裝依賴項。
這已在 NPM 7.5.3
及更高版本中修復。
這已被確認為 NPM 7.x
上的錯誤。 該團隊目前正在使用他們的GitHub Pull Request #2602修復該錯誤。 在此錯誤修復發布之前,最好的建議是執行下面列出的解決方法之一。
--registry
命令行標志運行npm publish
時,添加--registry
標志。 此標志使開發人員能夠指定他們打算發布到的注冊表,覆蓋.npmrc
或package.json
配置。 對於我的問題中列出的示例,項目.npmrc
應包含:
@my-org:registry=https://npm.pkg.github.com/
通過運行啟動發布將成功:
npm publish --registry=https://npm.pkg.github.com/
根據npm 開發人員 wraithgar 對此 bug 的 GitHub 問題的評論,您可以使用默認 NPM 注冊表的虛擬令牌來解決此問題。 在我們項目的.npmrc
中,添加以下行:
//registry.npmjs.org/:_authToken=dummy
完整的項目.npmrc
應該包含:
//registry.npmjs.org/:_authToken=dummy
@my-org:registry=https://npm.pkg.github.com/
在這個問題中,我們嘗試使用的項目.npmrc
語法都沒有指定注冊表,因為 NPM 的文檔聲明我們不必這樣做。 文檔指出,如果我們的項目.npmrc
中沒有,它將檢查我們的全局~/.npmrc
.npmrc 。 NPM 中的錯誤導致 NPM 在嘗試進行身份驗證之前檢查項目.npmrc
是否已指定注冊表。 用wraithgar 自己的話來說:
發生的情況是,當前 cli 僅在查看您是否已登錄時查找您配置的“注冊表”設置。因此臨時解決方案是覆蓋該設置(就像您通過傳遞 --registry 所做的那樣),或者添加默認 (npm) 注冊表的令牌,以便令牌檢查不會失敗。 檢查只是尋找配置中是否存在令牌,而不是驗證它(這當然會在實際請求期間使用它時發生),因此放入一個虛擬值將停止錯誤,直到 PR土地。
添加虛擬令牌后,運行npm publish
和npm install
都應該成功。
我有同樣的錯誤。 我修復的第一件事是 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`
在嘗試了所有建議的解決方法后,它仍然給出了相同的 output。 最后我找到了原因:我忘記在package.json“名稱”屬性中添加scope,所以問題通過更改解決了
{
"name": "my-package",
...
至
{
"name": "@my-org/my-package",
...
我從 1 到 7 的步驟中發現了 2 個問題。
package.json
中添加以下字段 "publishConfig": {
"registry": "https://npm.pkg.github.com/"
}
.npmrc
中,我沒有看到//npm.pkg.github.com/:_authToken=<auth-token-used-for-login>
,記住//
不是注釋。 ;
或#
用於評論。https://docs.npmjs.com/cli/v6/configuring-npm/npmrc#comments
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.