升级至Angular 16后无法安装Firebase:npm出现ERESOLVE错误
问题
Angular从v15升级到v16后,执行npm install firebase @angular/fire时触发依赖冲突错误,具体报错如下:
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: morish-wilson@0.0.0
npm ERR! Found: @angular/common@16.0.2
npm ERR! node_modules/@angular/common
npm ERR! @angular/common@"^16.0.0" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @angular/common@"^12.0.0 || ^13.0.0 || ^14.0.0 || ^15.0.0" from @angular/fire@7.5.0
npm ERR! node_modules/@angular/fire
npm ERR! @angular/fire@"*" 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.
原因
当前安装的@angular/fire@7.5.0版本仅支持Angular 12~15,不兼容Angular 16,导致peer依赖校验失败。
解决办法
1. 安装兼容Angular 16的@angular/fire版本
Angular Fire 7.6.0及以后版本已适配Angular 16,执行以下命令安装最新兼容版本:
npm install firebase @angular/fire@latest
也可以直接指定具体兼容版本:
npm install firebase @angular/fire@7.6.0
2. 临时跳过peer依赖检查(不推荐)
如果暂时无法升级@angular/fire,可使用--legacy-peer-deps参数绕过依赖冲突,但可能引发后续兼容性问题,仅作为临时方案:
npm install firebase @angular/fire --legacy-peer-deps
内容的提问来源于stack exchange,提问作者Morish Wilson

