Laravel UI React项目执行npm install时依赖冲突报错求助
依赖冲突问题:Vite 4.x 与 @vitejs/plugin-react 2.2.0 不兼容
执行npm install时出现以下错误:
npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\E495\AppData\Local\npm-cache\_logs\2023-01-16T08_08_36_872Z-debug-0.log PS C:\Users\E495\Desktop\laravel-simple-crud> npm install npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead. npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: undefined@undefined npm ERR! Found: vite@4.0.4 npm ERR! node_modules/vite npm ERR! dev vite@"^4.0.4" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer vite@"^3.0.0" from @vitejs/plugin-react@2.2.0 npm ERR! node_modules/@vitejs/plugin-react npm ERR! dev @vitejs/plugin-react@"^2.2.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\E495\AppData\Local\npm-cache\eresolve-report.txt for a full report.
当前项目的package.json配置:
{ "private": true, "scripts": { "dev": "vite", "build": "vite build" }, "devDependencies": { "@popperjs/core": "^2.11.6", "@tailwindcss/forms": "^0.5.2", "@vitejs/plugin-react": "^2.2.0", "alpinejs": "^3.4.2", "autoprefixer": "^10.4.2", "axios": "^1.1.2", "bootstrap": "^5.2.3", "laravel-vite-plugin": "^0.7.2", "lodash": "^4.17.19", "postcss": "^8.4.6", "react": "^18.2.0", "react-dom": "^18.2.0", "sass": "^1.56.1", "tailwindcss": "^3.1.0", "vite": "^4.0.4" } }
解决办法
方案1:升级@vitejs/plugin-react到兼容Vite 4.x的版本
修改package.json中@vitejs/plugin-react的版本为^4.0.0(或更高兼容版本):
"@vitejs/plugin-react": "^4.0.0"
然后执行:
npm install
方案2:降级Vite到3.x版本
修改package.json中vite的版本为^3.2.7(或其他3.x兼容版本):
"vite": "^3.2.7"
然后执行:
npm install
方案3:临时跳过依赖检查(不推荐长期使用)
如果只是想临时安装依赖,可以执行:
npm install --legacy-peer-deps
或者:
npm install --force
注意:这种方式可能会引入潜在的兼容性问题,仅作为临时方案。
内容的提问来源于stack exchange,提问作者Achraf Hebboul
相关产品推荐
相关产品推荐

