使用Capacitor构建Ionic项目时遇ERESOLVE依赖冲突(rxjs版本问题)
解决Ionic+Capacitor构建Android时的rxjs依赖冲突问题
问题描述
用Capacitor构建按功能分包的Ionic项目时,执行ionic capacitor build android命令失败,空白项目构建正常。报错核心是npm依赖解析冲突:
- 当前项目使用
rxjs@7.4.0(适配Angular 15) @ionic-native/core@5.36.0要求的peer依赖是rxjs@^5.5.0 || ^6.5.0,版本不兼容
报错详情:
npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: @ionic-native/core@5.36.0 npm ERR! Found: rxjs@7.4.0 npm ERR! node_modules/rxjs npm ERR! peer rxjs@"^6.5.3 || ^7.4.0" from @angular/common@15.0.2 npm ERR! node_modules/@angular/common npm ERR! peer @angular/common@"15.0.2" from @angular/forms@15.0.2 npm ERR! node_modules/@angular/forms npm ERR! peer @angular/forms@">=12.0.0" from @ionic/angular@6.3.9 npm ERR! node_modules/@ionic/angular npm ERR! @ionic/angular@"^6.3.9" from the root project npm ERR! 1 more (the root project) npm ERR! peer @angular/common@"15.0.2" from @angular/platform-browser@15.0.2 npm ERR! node_modules/@angular/platform-browser npm ERR! peer @angular/platform-browser@"15.0.2" from @angular/forms@15.0.2 npm ERR! node_modules/@angular/forms npm ERR! peer @angular/forms@">=12.0.0" from @ionic/angular@6.3.9 npm ERR! node_modules/@ionic/angular npm ERR! 1 more (the root project) npm ERR! 3 more (@angular/platform-browser-dynamic, @angular/router, the root project) npm ERR! 3 more (@angular/platform-browser-dynamic, @angular/router, the root project) npm ERR! peer rxjs@"^6.5.3 || ^7.4.0" from @angular/core@15.0.2 npm ERR! node_modules/@angular/core npm ERR! peer @angular/core@"15.0.2" from @angular/common@15.0.2 npm ERR! node_modules/@angular/common npm ERR! peer @angular/common@"15.0.2" from @angular/forms@15.0.2 npm ERR! node_modules/@angular/forms npm ERR! peer @angular/forms@">=12.0.0" from @ionic/angular@6.3.9 npm ERR! node_modules/@ionic/angular npm ERR! 1 more (the root project) npm ERR! 4 more (@angular/platform-browser, ...) npm ERR! peerOptional @angular/core@"15.0.2" from @angular/compiler@15.0.2 npm ERR! node_modules/@angular/compiler npm ERR! peer @angular/compiler@"15.0.2" from @angular/compiler-cli@15.0.2 npm ERR! node_modules/@angular/compiler-cli npm ERR! peer @angular/compiler-cli@"^15.0.0" from @angular-devkit/build-angular@15.0.2 npm ERR! node_modules/@angular-devkit/build-angular npm ERR! 2 more (@ngtools/webpack, the root project) npm ERR! 2 more (@angular/platform-browser-dynamic, the root project) npm ERR! 7 more (@angular/forms, @angular/platform-browser, ...) npm ERR! 5 more (@angular/forms, @angular/router, @ionic/angular, ...) npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer rxjs@"^5.5.0 || ^6.5.0" from @ionic-native/core@5.36.0 npm ERR! node_modules/@ionic-native/core npm ERR! peer @ionic-native/core@"^5.1.0" from @ionic-native/network@5.36.0 npm ERR! node_modules/@ionic-native/network npm ERR! @ionic-native/network@"^5.36.0" from the root project npm ERR! npm ERR! Conflicting peer dependency: rxjs@6.6.7 npm ERR! node_modules/rxjs npm ERR! peer rxjs@"^5.5.0 || ^6.5.0" from @ionic-native/core@5.36.0 npm ERR! node_modules/@ionic-native/core npm ERR! peer @ionic-native/core@"^5.1.0" from @ionic-native/network@5.36.0 npm ERR! node_modules/@ionic-native/network npm ERR! @ionic-native/network@"^5.36.0" 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.
解决方案
方案一:升级@ionic-native到兼容rxjs7的版本(推荐)
@ionic-native 5.x基于Cordova,不兼容rxjs7;6.x版本适配了Capacitor和rxjs7,完全匹配你的Angular15环境:
- 卸载旧的@ionic-native包
npm uninstall @ionic-native/core @ionic-native/network - 安装最新的6.x版本(同时安装Capacitor原生插件,因为Ionic Native 6+是对Capacitor插件的封装)
npm install @ionic-native/core @ionic-native/network @capacitor/network - 重新执行构建命令
ionic capacitor build android
方案二:临时绕过依赖检查(不推荐)
如果暂时无法升级包,可使用npm的--legacy-peer-deps参数跳过peer依赖冲突检测:
npm install --legacy-peer-deps ionic capacitor build android
注意:此方法可能导致运行时潜在问题,仅作为临时应急方案。
内容的提问来源于stack exchange,提问作者Haku
相关产品推荐
相关产品推荐

