簡體   English   中英

修復安裝 NPM 包的上游依賴沖突

[英]Fix the upstream dependency conflict installing NPM packages

我正在嘗試 npm 安裝 vue-mapbox mapbox-gl,但出現依賴關系樹錯誤。

我正在使用 Vuetify 運行Nuxt.js SSR,並且在運行此安裝之前沒有安裝任何與 Mapbox 相關的東西,並且收到此錯誤。

38 error code ERESOLVE
39 error ERESOLVE unable to resolve dependency tree
40 error
41 error While resolving: [1mexample[22m@[1m1.0.0[22m
41 error Found: [1mmapbox-gl[22m@[1m1.13.0[22m[2m[22m
41 error [2mnode_modules/mapbox-gl[22m
41 error   [1mmapbox-gl[22m@"[1m^1.13.0[22m" from the root project
41 error
41 error Could not resolve dependency:
41 error [35mpeer[39m [1mmapbox-gl[22m@"[1m^0.53.0[22m" from [1mvue-mapbox[22m@[1m0.4.1[22m[2m[22m
41 error [2mnode_modules/vue-mapbox[22m
41 error   [1mvue-mapbox[22m@"[1m*[22m" from the root project
41 error
41 error Fix the upstream dependency conflict, or retry
41 error this command with --force, or --legacy-peer-deps
41 error to accept an incorrect (and potentially broken) dependency resolution.
41 error
41 error See /Users/user/.npm/eresolve-report.txt for a full report.
42 verbose exit 1

go 關於解決這個上游依賴沖突的正確方法是什么?

看起來這是最新版本的 npm (v7) 中的 Peer Dependencies 的問題,該版本仍然是 beta 版本。 嘗試使用npm install --legacy-peer-deps了解詳細信息檢查此https://blog.npmjs.org/post/626173315965468672/npm-v7-series-beta-release-and-semver-major

npm install之后使用--legacy-peer-deps 例如,如果要安裝鐳,請使用:

npm install --legacy-peer-deps --save radium

有兩種方式:

  1. 使用npm install --legacy-peer-deps安裝,如果這不起作用,請使用

  2. 力法。 在 npm install 旁邊添加 --force: npm install --force

直到 npm 版本 7.19.1 仍然存在相同的問題,升級到版本 7.20.3后使用命令npm install -g npm@latestnpm audit fix所有 pkg​​s 修復沒有錯誤。

你可以按照這個命令

第一種:

npm config set legacy-peer-deps true

然后輸入:

npx create-react-app my-app

解決 修復安裝 NPM 包的上游依賴沖突錯誤

方法 1.在 npm install 之后使用 --legacy-peer-deps。

例如,如果要安裝 axios,請使用

npm install --legacy-peer-deps --save axios.

方法 2.更新 npm 和審計修復

npm I -g npm@latest
npm audit fix --force

方法 3.使用 --force 安裝包

npm install axios --force

我嘗試了多種方法,但沒有什么對我有用。 最后嘗試了這個並且它工作了npm config set legacy-peer-deps true在項目文件夾中運行這個然后嘗試安裝任何包。 也可能對你有用

要解決 npm 依賴項和與 npm 包的沖突,請使用 npm-check-updates https://www.npmjs.com/package/npm-check-updates

  • 刪除 package-lock.json 文件
  • 修改 package.json ,更新對等依賴所指示的版本
  • 運行 npm install 或 npm udpate

發生的事情是您的依賴mexample需要mmapbox-gl v1.13.0 而mvue-mapbox需要 v0.53.0。

NPM 真的不知道該怎么做,所以它會發出警告。 您可以使用-- force--legacy-peer-deps繞過錯誤,但您忽略了錯誤。

生產選項:

  1. 可能您的其中一個軟件包已過時。 升級包和修復升級錯誤可能會修復依賴沖突。

  2. 手動覆蓋依賴項以避免警告和錯誤。 您正在做的是將版本設置為您知道有效的特定版本。 通常是較新的版本。

具有覆蓋的示例解決方案。 您的 package.json 將如下所示:

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "mexample": "^1.2.0",
    "vue-mapbox": "*"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "overrides": {
    "mmapbox-gl": "1.13.0"
  }
}

最后一個選項是使用以下任一方法繞過:

  1. --legacy-peer-deps使用最新版本完全忽略所有peerDependencies而不固定在 package-lock.json
  2. --force強制使用最新的固定 package-lock.json 上的所有版本

額外:您不應該使用“*”作為版本,因為可能會更新主要和破壞依賴關系。

使用 npm install --legacy-peer-deps

我已將我的節點版本降級為10.23.1 ,它運行良好。

很多人贊成使用--legacy-peer-deps但如果--force有效,我建議使用它,因為它仍然固定許多依賴版本,而--legacy-peer-deps完全忽略對等依賴項。 請參見下面的示例:

https://stackoverflow.com/a/66608842/3850405

幾天前開始在 Azure DevOps 上收到此錯誤。 最初認為這是 Azure 方面的一個小故障,但由於它繼續存在,我們開始對其進行更多調查。

原來我們使用的代理windows-2022是幾天前更新的。

https://github.com/actions/virtual-environments/commit/2950cbfeab88a6a6202fa31d7371e574dbe2dc51

Node 和 NPM 現在匹配最新的 Node.js LTS 版本:16.15.1(包括 npm 8.11.0)

https://nodejs.org/en/download/

您可以在此處查看所有代理軟件:

https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops&tabs=yaml#software

在閱讀 Microsoft Visual Studio 開發人員社區后,他們建議使用Node.js Tool Installer task降級 Node.js,如下所示:

- task: NodeTool@0
  inputs:
    versionSpec: '16.14.2' 

https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/tool/node-js?view=azure-devops

https://developercommunity.visualstudio.com/t/npm-install-fails-in-azure-devops-hosted-agent/1606509

但是我們決定不想降級 Node.js,所以第一步是在本地匹配 Node.js 與 LTS 版本:16.15.1 和 npm 8.11.0。

在運行npm ci時,我們在本地遇到了同樣的錯誤。

嘗試了npm ci --force ,然后我們得到了這個錯誤:

npm ci只能在您的 package.json 和 package-lock.json 或 npm-shrinkwrap.json 同步時安裝包。 請在繼續之前使用npm install更新您的鎖定文件。

即使在手動刪除node_modulesnpm install npm install --force工作並生成了新的package-lock.json之后, npm install 也會出現相同的錯誤。

npm ci仍然失敗並出現同樣的錯誤,但運行npm ci --force有效。 我們決定更新 Azure DevOps .yml以包含--force並簽入新的package-lock.json 完成此操作后,一切都像以前一樣工作,我們現在可以一個一個地更新我們的包。

我通過添加解決了這個問題

 steps: - task: NodeTool@0 inputs: versionSpec: '12.x'

我長期被困在這個問題上,這也會導致其他命令出錯,這些命令需要一些破壞性的安裝命令。

唯一有效的解決方案(可能會抑制錯誤)是

npm config set legacy-peer-deps true

這會將legacy-peer-deps的配置設置為true

這肯定有效

npm 配置設置 legacy-peer-deps true

運行它然后用 npm 安裝任何東西

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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