尝试使用 npm 安装 mui core 时出错

Error while trying to install mui core with npm

我刚刚部署了一个新的 create-react-app,但仍然收到同样的错误消息。我安装了 MUI 并收到 'unable to resolve dependency tree'

    npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: eat-blended-web@0.1.0
npm ERR! Found: react@18.1.0
npm ERR! node_modules/react
npm ERR!   react@"^18.1.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.8.0 || ^17.0.0" from @material-ui/core@4.12.4
npm ERR! node_modules/@material-ui/core
npm ERR!   @material-ui/core@"*" 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 /Users/rodriguezmedia/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/rodriguezmedia/.npm/_logs/2022-05-13T00_57_27_262Z-debug-0.log
rodriguezmedia@Frankies-MacBook-Air-2 eat-blended-web % 

错误告诉您,您尝试安装的软件包在其对等依赖项中有 react@"^16.8.0 || ^17.0.0",但您目前正在使用 react@18.1.0

您可以通过在命令末尾包含 --legacy-peer-deps 来解决这个问题并安装您的软件包。例如:

npm install @mui/material --legacy-peer-deps

请记住,对等依赖项是包设计用于使用的模块。使用此标志继续安装可能会导致意外更改,有时甚至是破坏性更改。由于 React 18 是新的,一些包还没有更新以专门将它包含在它们的对等依赖项中。

而不是使用“npm install @material-ui/core”, 只需参考“npm install @mui/material” 对于 mui 图标,只需安装“npm install @mui/icons-material”。

有关更多详细信息,请转到“https://www.npmjs.com/package/@mui/icons-material”此页面。