Angular新建项目删除node_modules后报npm ERESOLVE依赖错误
问题场景
Angular项目删除node_modules文件夹与package-lock.json文件后,执行npm依赖安装操作失败,抛出ERESOLVE依赖解析错误。
错误日志如下:
npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: angular-front-end@0.0.0 npm ERR! Found: jasmine-core@3.4.0 npm ERR! node_modules/jasmine-core npm ERR! dev jasmine-core@"~3.4.0" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer jasmine-core@">=3.8" from karma-jasmine-html-reporter@1.7.0 npm ERR! node_modules/karma-jasmine-html-reporter npm ERR! dev karma-jasmine-html-reporter@"^1.4.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. npm ERR! npm ERR! See C:\Users\kurum\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\kurum\AppData\Local\npm-cache_logs\2022-06-25T12_48_31_942Z-debug-0.log Package install failed, see above.
错误截图:
错误根因
npm 7.x及以上版本默认开启严格peer依赖校验规则。当前项目在package.json中声明的开发依赖jasmine-core版本为~3.4.0,但自动拉取的高版本karma-jasmine-html-reporter@1.7.0要求宿主环境安装的jasmine-core版本必须大于等于3.8,版本要求不匹配导致依赖树解析中断。
解决方法
- 方案1(推荐,无兼容隐患):升级jasmine-core到匹配版本
执行以下命令将jasmine-core升级到符合peer依赖要求的版本:npm install jasmine-core@~3.8.0 --save-dev
命令执行完成后重新运行npm install,即可正常完成全量依赖安装。 - 方案2:降级karma-jasmine-html-reporter到适配现有jasmine-core的版本
打开项目根目录的package.json,找到devDependencies配置项里的karma-jasmine-html-reporter,将版本号从^1.4.0修改为1.5.4——该版本对jasmine-core的peer依赖要求为>=3.3,和现有3.4.0版本完全适配。修改保存后重新执行npm install即可。 - 方案3(临时应急,不推荐长期使用):跳过严格peer依赖校验
执行安装命令时追加--legacy-peer-deps参数,复用npm 6.x版本的依赖解析逻辑,不强制校验peer依赖版本匹配:npm install --legacy-peer-deps
该方式可能埋下测试组件运行时的兼容隐患,仅适合临时快速启动项目时使用。
注意:不建议使用
--force参数强制安装,该参数会跳过所有依赖校验逻辑、强制覆盖冲突文件,极易造成后续依赖版本混乱,引发难以排查的运行时错误。
内容的提问来源于stack exchange,提问作者Srinivas Kurumalla
相关产品推荐
相关产品推荐

