React项目安装Material UI时遭遇TypeScript版本依赖冲突问题求助
问题背景
我在给React项目安装Material UI的时候碰到了依赖冲突的报错,目前我的项目环境是React v18.2.0 + TypeScript v5.2.2,执行的安装命令是:
npm install @mui/material @emotion/react @emotion/styled
报错信息
执行命令后得到的完整错误输出如下:
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: react-scripts@5.0.1
npm ERR! Found: typescript@5.2.2
npm ERR! node_modules/typescript
npm ERR! peer typescript@">= 2.7" from fork-ts-checker-webpack-plugin@6.5.2
npm ERR! node_modules/fork-ts-checker-webpack-plugin
npm ERR! fork-ts-checker-webpack-plugin@"^6.5.0" from react-dev-utils@12.0.1
npm ERR! node_modules/react-dev-utils
npm ERR! react-dev-utils@"^12.0.1" from react-scripts@5.0.1
npm ERR! node_modules/react-scripts
npm ERR! react-scripts@"5.0.1" from the root project
npm ERR! peer typescript@">=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta" from tsutils@3.21.0
npm ERR! node_modules/tsutils
npm ERR! tsutils@"^3.21.0" from @typescript-eslint/eslint-plugin@5.48.2
npm ERR! node_modules/@typescript-eslint/eslint-plugin
npm ERR! @typescript-eslint/eslint-plugin@"^5.5.0" from eslint-config-react-app@7.0.1
npm ERR! node_modules/eslint-config-react-app
npm ERR! eslint-config-react-app@"^7.0.1" from react-scripts@5.0.1
npm ERR! node_modules/react-scripts
npm ERR! 1 more (the root project)
npm ERR! 1 more (eslint-plugin-jest)
npm ERR! tsutils@"^3.21.0" from @typescript-eslint/type-utils@5.48.2
npm ERR! node_modules/@typescript-eslint/type-utils
npm ERR! @typescript-eslint/type-utils@"5.48.2" from @typescript-eslint/eslint-plugin@5.48.2
npm ERR! node_modules/@typescript-eslint/eslint-plugin
npm ERR! @typescript-eslint/eslint-plugin@"^5.5.0" from eslint-config-react-app@7.0.1
npm ERR! node_modules/eslint-config-react-app
npm ERR! 1 more (eslint-plugin-jest)
npm ERR! 1 more (@typescript-eslint/typescript-estree)
npm ERR! 1 more (the root project)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peerOptional typescript@"^3.2.1 || ^4" from react-scripts@5.0.1
npm ERR! node_modules/react-scripts
npm ERR! react-scripts@"5.0.1" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: typescript@4.9.5
npm ERR! node_modules/typescript
npm ERR! peerOptional typescript@"^3.2.1 || ^4" from react-scripts@5.0.1
npm ERR! node_modules/react-scripts
npm ERR! react-scripts@"5.0.1" 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.
我的项目依赖配置
项目package.json里的依赖如下:
"dependencies": { "@arcgis/core": "^4.26.5", "@azure/msal-browser": "^2.32.2", "@azure/msal-react": "^1.5.2", "@esri/calcite-components-react": "^1.0.5", "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", "@types/jest": "^29.5.8", "@types/node": "^20.9.2", "@types/react": "^18.2.37", "@types/react-dom": "^18.2.15", "bootstrap-icons": "^1.10.3", "http-proxy-middleware": "^2.0.6", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.8.2", "react-scripts": "5.0.1", "recharts": "^2.5.0", "typescript": "^5.2.2", "web-vitals": "^2.1.4" }, "devDependencies": { "eslint-config-react-app": "^7.0.1", "jest-editor-support": "^31.0.1" }
我的疑问
我看报错里提到有的依赖要求TypeScript >=2.7,我现在用的5.2.2明明远高于这个版本啊,为啥还会触发依赖冲突?是不是我没读懂报错的核心原因?
更新&解决方案
感谢管理员的建议,我按照以下步骤顺利解决了问题:
- 步骤1:修改
package.json里的TypeScript版本,将typescript设置为^4.x.x(比如^4.9.5) - 步骤2:删除项目根目录下的
package-lock.json文件和node_modules文件夹 - 步骤3:执行命令
npm install,确保项目完全切换到TypeScript 4版本 - 步骤4:再次执行Material UI的安装命令:
npm install @mui/material @emotion/react @emotion/styled
备注:内容来源于stack exchange,提问作者LNX

