【问题标题】:Overriding peer dependency error on npm install在 npm install 上覆盖对等依赖项错误
【发布时间】:2022-11-10 00:01:22
【问题描述】:

我正在尝试运行npm install @react-navigation/native @react-navigation/native-stack,但这样做时最终收到这些错误:

npm WARN ERESOLVE overriding peer dependency
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: react-native-web@0.17.1
npm ERR! Found: react@16.13.1
npm ERR! node_modules/react
npm ERR!   peer react@"^17.0.0" from react-freeze@1.0.0
npm ERR!   node_modules/react-native-screens/node_modules/react-freeze
npm ERR!     react-freeze@"^1.0.0" from react-native-screens@3.13.1
npm ERR!     node_modules/react-native-screens
npm ERR!       peer react-native-screens@">= 3.0.0" from @react-navigation/native-stack@6.6.2
npm ERR!       node_modules/@react-navigation/native-stack
npm ERR!         @react-navigation/native-stack@"*" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@">=17.0.1" from react-native-web@0.17.1
npm ERR! node_modules/react-native-web
npm ERR!   react-native-web@"^0.17.1" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: react@18.2.0
npm ERR! node_modules/react
npm ERR!   peer react@">=17.0.1" from react-native-web@0.17.1
npm ERR!   node_modules/react-native-web
npm ERR!     react-native-web@"^0.17.1" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!
npm ERR! See /home/reptar/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/reptar/.npm/_logs/2022-06-15T11_49_30_010Z-debug-0.log

这是我的 package.json 文件:

  {
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "axios": "^0.21.4",
    "expo": "~42.0.1",
    "expo-status-bar": "~1.0.4",
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "react-native": "https://github.com/expo/react-native/archive/sdk-42.0.0.tar.gz",
    "react-native-select-dropdown": "^1.0.9",
    "react-native-web": "^0.17.1"
  },
  "devDependencies": {
    "@babel/core": "^7.9.0"
  },
  "private": true
}

有人可以指导我如何解决这个问题吗?当我必须安装或修复依赖项/包时,这是我最挣扎的事情。

=============================更新:

当我尝试更新 react 时,我必须同时更新 react-dom,否则会出现类似的错误。所以我跑了npm i react@latest react-dom@latest。然后我会尝试再次运行导航安装并会收到以下错误

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: undefined@undefined
npm ERR! Found: react@18.2.0
npm ERR! node_modules/react
npm ERR!   react@"^18.2.0" from the root project
npm ERR!   peer react@"*" from @react-navigation/native@6.0.10
npm ERR!   node_modules/@react-navigation/native
npm ERR!     @react-navigation/native@"*" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"16.13.1" from react-native@0.63.2
npm ERR! node_modules/react-native
npm ERR!   react-native@"https://github.com/expo/react-native/archive/sdk-42.0.0.tar.gz" from the root project
npm ERR!   peer react-native@"*" from @react-navigation/native@6.0.10
npm ERR!   node_modules/@react-navigation/native
npm ERR!     @react-navigation/native@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

【问题讨论】:

  • 我有同样的问题,它工作正常,但我安装了一个新的操作系统,然后再次安装所有内容,然后再次使用该项目,当我执行 npm install 时,我得到了同样的错误

标签: javascript node.js react-native npm peer-dependencies


【解决方案1】:

选项 1 - 忽略上游依赖,后果自负(根据错误消息):

npm install @react-navigation/native @react-navigation/native-stack --legacy-peer-deps

选项 2 - 满足上游依赖的 react 的更新版本 (react@">=17.0.1"):

npm install react@17.0.1 或最新版本 npm install react@latest

接着...

npm install @react-navigation/native @react-navigation/native-stack

在您更新之后,现在从 peer react-dom@">=17.0.1" from react-native-web@0.17.1 行可以清楚地看到您应该使用 npm install react-dom@17.0.1 或更高版本。

预期的对等依赖版本在错误消息中 - 使用语义版本控制。

【讨论】:

  • 我不想选择 opt 1,因为我不赞成强制安装不能完全解决问题的安装。我尝试了选项 2,但仍然出现错误;查看我的问题中的更新部分。 --legacy-peer-deps 会为这部分工作吗?
  • 我实际上不确定--legacy-peer-deps 是如何工作的,但它主要是为了帮助防止添加与react 17.0.1 不兼容的模块吗?
  • 也许阅读这篇文章 - flaviocopes.com/npm-peer-dependencies “当一个依赖项作为 peerDependency 列在包中时,它不会自动安装。相反,包含该包的代码必须包含它作为它的依赖项。”这就是下游和上游的来源。基本上,具有对等依赖关系的包将需要在托管项目中安装该依赖关系。
  • 请查看更新的解决方案。如果您阅读错误消息,您可以确定需要哪些对等依赖项版本。另一种方法是使用“npm update”更新项目中的所有包,看看是否能解决您的问题。作为旁注,我有一个 Angular 项目,我需要使用开关 --legacy-peer-deps 。
  • 更新后,它不允许我一次更新一个 react 和 react-dom,所以我在一行中完成了,但现在我被困在下一部分,即我的问题中的更新。在这一点上,我将使用 --legacy-peer-deps。这些安装每次都变得太多
【解决方案2】:

代替

"react-native-web": "^0.17.1"

至 “反应原生网络”:“^17.0.1”

看看它是否有效,认为你的 package.json 不匹配看到错误日志

peer react@">=17.0.1" from react-native-web@0.17.1

【讨论】:

    【解决方案3】:

    在你的包名后使用--legacy-peer-deps。 作为

    npm install your-packages --legacy-peer-deps
    

    【讨论】:

      猜你喜欢
      • 2021-04-30
      • 2015-11-16
      • 1970-01-01
      • 2016-09-08
      • 1970-01-01
      • 2015-11-21
      • 2018-04-22
      • 2018-11-01
      • 2018-09-18
      相关资源
      最近更新 更多