项目执行npm i 报错npm ERR code ERESOLVE

281 阅读2分钟

通常情况下,npm install 命令会根据项目中的 package.json 文件来安装所需的依赖项。但有时候,可能会出现一些问题,例如版本冲突或者安装过程中出现警告。

npm 版本问题会导致下载冲突,从而中断安装过程。npm i  下载依赖的时候出现以下类似的报错,大概就是版本的问题。

>npm ERR! code ERESOLVE  
npm ERR! ERESOLVE could not resolve  
npm ERR!   
npm ERR! While resolving: @vue/eslint-config-standard@6.1.0  
npm ERR! Found: eslint-plugin-vue@8.7.1  
npm ERR! node_modules/eslint-plugin-vue  
npm ERR!     dev eslint-plugin-vue@"^8.0.3" from the root project  
npm ERR!   
npm ERR! Could not resolve dependency:  
npm ERR! peer eslint-plugin-vue@"^7.0.0" from @vue/eslint-config-standard@6.1.0  
npm ERR! node_modules/@vue/eslint-config-standard  
npm ERR! Fix the upstream dependency conflict, or retry  
npm ERR! this command with --force, or --legacy-peer-deps
......

原因:可能是由于不兼容的依赖版本导致的问题。

解决:在命令后面加上 --force 或 --legacy-peer-deps参数

// 1.清除缓存
npm cache clean

// 若清除缓存时报错 则使用强制清除缓存
npm cache clean --force

/**
 * 若执行 npm cache clean --force 出现:
 * npm WARN using --force Recommended protections disabled.
 * 安装的npm版本过高 说明需要降低npm的版本了
 * 执行 :// 6.14.10 为版本号
 * npm 降低到指定版本:npm install npm@6.14.10 -g  
 * cnpm 降低到指定版本:npm install cnpm@6.14.10 -g 
 */

// 2. 执行安装
npm i --force

image.png

例:安装某个插件失败

image.png

在命令后面加上 --legacy-peer-deps 参数

image.png

总结

--force:表示强制安装,即使存在冲突或警告,也会强制执行安装操作。该参数可以绕过版本冲突或者安装过程中出现警告问题,强制安装依赖项。

--legacy-peer-deps:是在v7中引入的,目的是绕过peerDependency自动安装。  它告诉 NPM 忽略项目中引入的各个modules之间的相同modules但不同版本的问题并继续安装,保证各个引入的依赖之间对自身所使用的不同版本modules共存。