簡體   English   中英

`npm publish` 和 `npm install` 失敗取決於 `.npmrc` 使用的語法

[英]`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 installnpm publish兼容。 但是,根據我的預期用途,我似乎只能使用其中一種。

重現步驟:

  1. 通過nvm安裝 Node v15.7.0和 NPM 7.4.3

  2. 使用以下命令登錄 GitHub 包:

     npm login --scope=@my-org --registry=https://npm.pkg.github.com
  3. 檢查我們的主文件夾中的~/.npmrc文件。 它應該是:

     @my-org:registry=https://npm.pkg.github.com/ //npm.pkg.github.com/:_authToken=<auth-token-used-for-login>
  4. 使用以下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" } }
  5. 將以下.npmrc添加到我們的項目中:

     @my-org:registry=https://npm.pkg.github.com/
  6. 運行npm install 安裝應該成功。

  7. 運行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
  8. 將項目.npmrc更改為:

     registry=https://npm.pkg.github.com/my-org/
  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

環境:

  • 操作系統:
    • MacOS Catalina 10.15.7
    • MacOS 大蘇爾 11.1
  • 節點:15.7.0
  • npm:7.4.3

我嘗試過的事情

使用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/ ,我可以成功發布。 此外,我可以毫無問題地安裝依賴項。

更新(2021 年 5 月 30 日)

這已在 NPM 7.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/

通過運行啟動發布將成功:

npm publish --registry=https://npm.pkg.github.com/

解決方法 2:默認注冊表的虛擬令牌

根據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 publishnpm 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 個問題。

  1. 在第 4 步中,您是否嘗試在package.json中添加以下字段
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/"
  }
  1. 在第 5 步的.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM