修复上游依赖冲突安装 NPM 包

con*_*ode 52 javascript dependencies node.js npm mapbox

尝试 npm install vue-mapbox mapbox-gl 并且我收到依赖树错误。

我正在使用 Vuetify 运行 Nuxt 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
Run Code Online (Sandbox Code Playgroud)

解决这个上游依赖冲突的正确方法是什么?

Gus*_*cia 123

问题解释:

您的依赖项mexample需要mmapbox-glv1.13.0 和mvue-mapboxv0.53.0 mmapbox-gl

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"
  }
}
Run Code Online (Sandbox Code Playgroud)

绕过错误(快速而肮脏的解决方案):

  1. --legacy-peer-deps使用最新版本完全忽略所有对等依赖项,而不固定文件package-lock.json
  2. --force强制使用最新版本,将所有版本固定在package-lock.json上

额外:您不应使用“*”作为版本,因为它可能会更新主要版本并破坏依赖项。

  • 这是一个比这里大多数其他答案更好的答案。如果可以给我加分的话我愿意! (13认同)
  • 从字面上看,这是这个问题唯一明智的答案。彻底解释问题的原因,然后提出可行的解决方案以及每个方案背后的理由。干得好,先生。做得好。 (7认同)

小智 87

看起来这是最新版本的 npm (v7) 中 Peer Dependencies 的问题,该版本仍然是 beta 版本。尝试npm install --legacy-peer-deps详细信息检查这个https://blog.npmjs.org/post/626173315965468672/npm-v7-series-beta-release-and-semver-major

  • 事实上,这不是正确的答案。您在不了解原因和冲突的情况下强制使用版本,此警告不是无缘无故添加的。它可能会产生安全漏洞和意外的错误。更好的选择是了解冲突、修复它或固定要使用的版本。我在下面的答案中详细说明了。 (14认同)
  • [npm:何时使用 `--force` 和 `--legacy-peer-deps`](/sf/ask/4621457431/ -deps)可能会有所帮助 (3认同)

Md *_*lam 35

您可以按照这些命令

第一种:

npm config set legacy-peer-deps true
Run Code Online (Sandbox Code Playgroud)

然后输入:

npx create-react-app my-app
Run Code Online (Sandbox Code Playgroud)

  • 请也解释一下你的命令:( (5认同)

小智 25

我尝试了多种方法,但没有一个对我有用。最后我尝试了这个并且它有效:

npm config set legacy-peer-deps true
Run Code Online (Sandbox Code Playgroud)

在项目文件夹中运行它,然后尝试安装任何包。它可能也适合你。

  • 在“npm install”之前设置“npm config set Legacy-peer-deps true”对我有用。 (2认同)

小智 20

直到npm版本7.19.1,仍然存在同样的问题。升级到7.20.3版本后,使用命令npm install -g npm@latestnpm audit fix. 所有包都将被修复,不会出现错误。

  • 对我来说这个答案效果最好(Node v16.14.0,npm v8.3.1,Angular 13.2.4)。但我必须使用“--force”标志来运行它,例如“npmauditfix--force”。谢谢! (2认同)

小智 12

--legacy-peer-deps后使用npm install。例如,如果要安装镭,请使用npm install --legacy-peer-deps --save radium. 我希望它有效。


小智 10

有两种方式:

  1. 用于npm install --legacy-peer-deps安装,如果这不起作用,请使用

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


小智 7

我在这个问题上停留了很长时间,这也导致其他命令出错,这些命令调用了一些正在中断的安装命令。

唯一有效的解决方案(可能会抑制错误)是

npm config set legacy-peer-deps true
Run Code Online (Sandbox Code Playgroud)

这会将配置设置legacy-peer-depstrue


Nat*_*her 5

要解决 npm 依赖关系以及与 npm 包的冲突,请使用npm-check-updates

  • 我不知道这是否是OP所要求的,但这肯定是我在谷歌搜索时所想的:好吧,所以存在冲突。如何解决它们而不是使用命令行开关解决它们? (4认同)