安装react-native-reanimated后安装@react-navigation/drawer出现依赖冲突报错
你遇到的是npm 7及以上版本的严格peer依赖校验触发的报错,具体错误信息如下:
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency treenpm ERR! While resolving: app_mobile@0.0.1
npm ERR! Found: react-native-reanimated@2.3.0-beta.3
npm ERR! node_modules/react-native-reanimated
npm ERR! react-native-reanimated@"^2.3.0-beta.2" from the root projectnpm ERR! Could not resolve dependency:
npm ERR! peer react-native-reanimated@">= 1.0.0" from @react-navigation/drawer@6.1.8
npm ERR! node_modules/@react-navigation/drawer
npm ERR! @react-navigation/drawer@"*" from the root projectnpm 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! See C:\Users\dell\AppData\Local\npm-cache\eresolve-report.txt for a full report.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\dell\AppData\Local\npm-cache_logs\2021-10-21T08_17_02_306Z-debug.log
报错的核心原因是你本地安装的react-native-reanimated@2.3.0-beta.3属于beta预发布版本,npm的依赖解析规则判定预发布版本不满足>=1.0.0的正式版本区间要求,即使实际功能是兼容的,也会抛出冲突错误。
按照优先级从高到低给出以下可执行方案:
- 方案1(优先推荐):安装时添加参数绕过检查
无需修改你已安装的react-native-reanimated版本,执行以下命令安装drawer组件即可:npm install @react-navigation/drawer --legacy-peer-deps--legacy-peer-deps参数会关闭npm 7+的严格peer依赖校验逻辑,你当前安装的reanimated beta版本和@react-navigation/drawer@6.1.8实际功能兼容,用该参数安装不会产生运行问题。 - 方案2:替换react-native-reanimated为正式稳定版
如果你不想使用绕过参数,可以先卸载本地的beta版reanimated,换成正式发布的稳定版后再安装drawer:- 卸载beta版reanimated:
npm uninstall react-native-reanimated - 安装稳定版reanimated:
npm install react-native-reanimated@~2.2.4 - 正常安装drawer组件:
npm install @react-navigation/drawer
注意替换版本后,iOS端需要重新执行pod install,同时检查babel.config.js中是否已经配置了reanimated的对应插件。
- 卸载beta版reanimated:
- 方案3:强制安装
仅当方案1执行后仍报错时使用,执行以下命令强制忽略冲突安装:npm install @react-navigation/drawer --force
内容的提问来源于stack exchange,提问作者Nguyễn Như Thế Tài

