You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React@18.2.0与react-chartjs-2依赖冲突问题求助

React 18 + Material UI 依赖安装冲突解决方案

问题背景

我是React新手,用React 18.2.0和Material UI搭建网站,npm版本为8.19.3,安装/更新依赖时遇到ERESOLVE错误,具体报错如下:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: react-chartjs-2@3.0.4
npm ERR! Found: react@18.2.0
npm ERR! node_modules/react
npm ERR!   peer react@">=16.8.0" from @emotion/react@11.9.3
npm ERR!   node_modules/@emotion/react
npm ERR!     peer @emotion/react@"^11.0.0-rc.0" from @emotion/styled@11.9.3
npm ERR!     node_modules/@emotion/styled
npm ERR!       peerOptional @emotion/styled@"^11.3.0" from @mui/material@5.8.6
npm ERR!       node_modules/@mui/material
npm ERR!         peer @mui/material@"^5.0.0" from @mui/icons-material@5.8.4
npm ERR!         node_modules/@mui/icons-material
npm ERR!         1 more (the root project)
npm ERR!       4 more (@mui/styled-engine, @mui/system, @mui/styled-engine, the root project)
npm ERR!     peerOptional @emotion/react@"^11.5.0" from @mui/material@5.8.6
npm ERR!     node_modules/@mui/material
npm ERR!       peer @mui/material@"^5.0.0" from @mui/icons-material@5.8.4
npm ERR!       node_modules/@mui/icons-material
npm ERR!         @mui/icons-material@"5.8.4" from the root project
npm ERR!       1 more (the root project)
npm ERR!     4 more (@mui/styled-engine, @mui/system, @mui/styled-engine, the root project)
npm ERR!   peer react@">=16.8.0" from @emotion/styled@11.9.3
npm ERR!   node_modules/@emotion/styled
npm ERR!     peerOptional @emotion/styled@"^11.3.0" from @mui/material@5.8.6
npm ERR!     node_modules/@mui/material
npm ERR!       peer @mui/material@"^5.0.0" from @mui/icons-material@5.8.4
npm ERR!       node_modules/@mui/icons-material
npm ERR!         @mui/icons-material@"5.8.4" from the root project
npm ERR!       1 more (the root project)
npm ERR!     peerOptional @emotion/styled@"^11.3.0" from @mui/styled-engine@5.8.0
npm ERR!     node_modules/@mui/styled-engine
npm ERR!       @mui/styled-engine@"5.8.0" from the root project
npm ERR!     3 more (@mui/system, @mui/styled-engine, the root project)
npm ERR!   18 more (@mui/base, @mui/icons-material, @mui/material, ...)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.8.0 || ^17.0.0" from react-chartjs-2@3.0.4
npm ERR! node_modules/react-chartjs-2
npm ERR!   react-chartjs-2@"3.0.4" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: react@17.0.2
npm ERR! node_modules/react
npm ERR!   peer react@"^16.8.0 || ^17.0.0" from react-chartjs-2@3.0.4
npm ERR!   node_modules/react-chartjs-2
npm ERR!     react-chartjs-2@"3.0.4" 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\Admin\AppData\Local\npm-cache\eresolve-report.txt for a full report.

我的package.json配置:

{
"name": "react-ui",
"version": "4.0.0",
"author": "",
"license": "",
"description": "",
"engines": {
"node": "14 || 15 || 16",
"npm": ">=6"
},
"dependencies": {
"@emotion/cache": "11.9.3",
"@emotion/react": "11.9.3",
"@emotion/styled": "11.9.3",
"@mui/icons-material": "5.8.4",
"@mui/material": "5.8.6",
"@mui/styled-engine": "5.8.0",
"@testing-library/jest-dom": "5.16.4",
"@testing-library/react": "13.3.0",
"@testing-library/user-event": "14.2.1",
"chart.js": "3.4.1",
"chroma-js": "2.4.2",
"prop-types": "^15.8.1",
"react": "18.2.0",
"react-chartjs-2": "3.0.4",
"react-countup": "6.3.0",
"react-dom": "18.2.0",
"react-flatpickr": "3.10.13",
"react-router": "^6.8.0",
"react-router-dom": "6.3.0",
"react-scripts": "5.0.1",
"socket.io": "^4.5.4",
"socket.io-client": "^4.5.4",
"stylis": "4.1.1",
"stylis-plugin-rtl": "2.1.1",
"uuid": "8.3.2",
"web-vitals": "2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"install:clean": "rm -rf node_modules/ && rm -rf package-lock.json && npm install && npm start",
"install:peer-deps": "npm install --legacy-peer-deps"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"eslint": "8.18.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-prettier": "4.2.1",
"eslint-plugin-react": "7.30.1",
"eslint-plugin-react-hooks": "4.6.0",
"prettier": "2.7.1"
}
}

错误原因

核心冲突是react-chartjs-2@3.0.4仅支持React 16.8.x或17.x版本,但当前使用的是React 18.2.0,npm 7+默认严格校验peer依赖版本,因此抛出了ERESOLVE错误。

解决方案

方案1:升级react-chartjs-2到兼容React 18的版本

react-chartjs-2从v4.0.0开始正式支持React 18,修改package.json中的依赖版本:

"react-chartjs-2": "^4.0.0"

同时建议同步升级chart.js到兼容版本(v3.7.0+):

"chart.js": "^3.7.0"

修改后执行以下命令清理并重新安装依赖:

rm -rf node_modules package-lock.json
npm install

方案2:使用legacy-peer-deps参数跳过peer依赖校验

如果暂时不想升级依赖,可以使用npm的--legacy-peer-deps参数,它会回到npm 6的依赖解析逻辑,忽略peer版本冲突:

npm install --legacy-peer-deps

或者直接运行package.json中已定义的脚本:

npm run install:peer-deps

方案3:降级React到17.x版本(不推荐)

若坚持使用react-chartjs-2@3.0.4,可将React和ReactDOM降级到17.x版本,修改package.json:

"react": "^17.0.2",
"react-dom": "^17.0.2"

然后清理并重新安装依赖:

rm -rf node_modules package-lock.json
npm install

注意:此方案会错过React 18的新特性,且长期来看不如升级依赖更稳妥。

内容的提问来源于stack exchange,提问作者Amit Moghe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 16:25:25