You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React 18.2与React Native 0.70.6依赖冲突,求适配版本

React 18.2.0与React Native 0.70.6兼容性问题及解决方案

问题背景

新建React Native项目后,执行npm install react-native安装React Native 0.70.6时触发依赖解析错误。

错误信息

npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: react-native@0.70.6
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/core@6.4.5
npm ERR! node_modules/@react-navigation/core
npm ERR! @react-navigation/core@"^6.4.5" from @react-navigation/native@6.1.1
npm ERR! node_modules/@react-navigation/native
npm ERR! @react-navigation/native@"^6.1.1" from the root project
npm ERR! 3 more (@react-navigation/elements, ...)
npm ERR! 12 more (@react-navigation/elements, ...)

当前项目依赖配置

"dependencies": {
    "@react-navigation/native": "^6.1.1",
    "@react-navigation/native-stack": "^6.9.6",
    "@react-navigation/stack": "^6.3.9",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-native": "^0.70.6",
    "react-scripts": "5.0.1",
    "web-vitals": "^2.1.4"
  }

解决方案

兼容性说明

React Native 0.70.x系列不兼容React 18,它要求的React版本范围是^17.0.2。当前安装的React 18.2.0超出该范围,导致npm依赖解析失败。

适配步骤

  1. 降级React及相关依赖
    执行命令将React、React DOM和测试库降级到兼容版本:

    npm install react@^17.0.2 react-dom@^17.0.2 @testing-library/react@^12.1.5
    

    注意:@testing-library/react 13.x依赖React 18,必须同步降级到12.x版本才能匹配React 17。

  2. 清理依赖缓存(可选)
    若降级后仍有冲突,清理缓存并重新安装:

    npm cache clean --force
    rm -rf node_modules package-lock.json
    npm install
    
  3. 移除Web专属依赖
    项目中包含的react-scripts、web-vitals等是Web项目依赖,React Native无需使用,建议删除:

    npm uninstall react-scripts web-vitals @testing-library/jest-dom @testing-library/user-event
    

结论

React Native 0.70.6必须搭配React 17.x版本使用,按上述步骤调整依赖即可解决冲突,无需尝试其他兼容方案。

内容的提问来源于stack exchange,提问作者Andrew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 00:05:16