介绍
本篇文章主要记录 node14 升级 node18 遇到的问题
npm i 报错
ERESOLVE could not resolve 或RESOLVE unable to resolve dependency tree(无法解析依赖关系树)
当前项目安装的包有对等依赖冲突。
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: @nestjs/elasticsearch@8.1.0
npm ERR! Found: @elastic/elasticsearch@5.6.22
npm ERR! node_modules/@elastic/elasticsearch
npm ERR! @elastic/elasticsearch@"~5.6.22" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @elastic/elasticsearch@"^7.4.0 || ^8.0.0" from @nestjs/elasticsearch@8.1.0
npm ERR! node_modules/@nestjs/elasticsearch
npm ERR! @nestjs/elasticsearch@"~8.1.0" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: @elastic/elasticsearch@8.2.1
npm ERR! node_modules/@elastic/elasticsearch
npm ERR! peer @elastic/elasticsearch@"^7.4.0 || ^8.0.0" from @nestjs/elasticsearch@8.1.0
npm ERR! node_modules/@nestjs/elasticsearch
npm ERR! @nestjs/elasticsearch@"~8.1.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.
原因
npm7 将 install 方式默认为按照 peer dependencies 方式安装。
npm7之前不会中断安装过程,会保留两个版本,一个放在一级目录,一个放到项目的目录下;npm7 升级之后,所有 peerDependencies 会自动安装,但是如何某个包的 peerDependencies 和 root 层同样报的版本冲突,会自动报错。
解决方法
- 按照报错信息解决包版本冲突
截图中的报错只是一个示例,并不一定都是 react 这个包报错版本冲突,大部分包都会报错版本冲突。
- 如果是内部的包,可以直接在内部包加 peer 解决版本冲突或者解决版本冲突
- 外部 npm 的包,可以去看一下版本是否可以兼容
- 冲突太多无法暂时全部解决的,可以暂时使用下面的方式
- npm install --force 或 npm install --legacy-peer-deps
- .npmrc 文件配置 legacy-peer-deps = true
npm i -- force // 强制 npm 从远端获取资源,即使磁盘上存在副本
npm i --legacy-peer-deps // 绕过 peerDependency里的依赖自动安装,它告诉npm忽略项目中引入的各个依赖模块之前依赖相同但版本不同的问题,以npmV3-V6的方式继续执行安装操作。所以其实该命令并没有真的解决冲突,而是忽略了冲突,以“过时”(v3-v6)的方式进行下载操作
使用上述方法后,不会弹出报错信息,只是直接跳过安装依赖,不能解决包的使用问题。解决问题的办法就是手动在顶层安装冲突的依赖,虽然出现一些 warning 信息,但是安装的组件可以正常使用