Material UI Icons npm 安装问题:无法解析依赖树

Ank*_*t_M 16 dependencies npm reactjs material-ui

---------问题已解决 - 问题末尾的解决方案--------

我尝试使用 npm 在 Netflix-Clone React.js 项目中安装Material UI Icons,但出现以下错误 -

PS D:\REACT Projects\netflix> npm i @mui/icons-material @mui/material
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: netflix@0.1.0
npm ERR! Found: react@18.0.0
npm ERR! node_modules/react
npm ERR!   react@"^18.0.0" from the root project
npm ERR!   peer react@">=16.8.0" from @emotion/react@11.8.2
npm ERR!   node_modules/@emotion/react
npm ERR!     peerOptional @emotion/react@"^11.5.0" from @mui/material@5.5.3
npm ERR!     node_modules/@mui/material
npm ERR!       @mui/material@"^5.5.3" from the root project
npm ERR!       1 more (@mui/icons-material)
npm ERR!     peer @emotion/react@"^11.0.0-rc.0" from @emotion/styled@11.8.1
npm ERR!     node_modules/@emotion/styled
npm ERR!       peerOptional @emotion/styled@"^11.3.0" from @mui/material@5.5.3
npm ERR!       node_modules/@mui/material
npm ERR!         @mui/material@"^5.5.3" from the root project
npm ERR!         1 more (@mui/icons-material)
npm ERR!   1 more (@emotion/styled)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^17.0.0" from @mui/material@5.5.3
npm ERR! node_modules/@mui/material
npm ERR!   @mui/material@"^5.5.3" from the root project
npm ERR!   peer @mui/material@"^5.0.0" from @mui/icons-material@5.5.1
npm ERR!   node_modules/@mui/icons-material
npm ERR!     @mui/icons-material@"^5.5.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.
Run Code Online (Sandbox Code Playgroud)

以下是我的package.json文件 -

{
  "name": "netflix",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@mui/icons-material": "^5.5.1",
    "@mui/material": "^5.5.3",
    "@testing-library/jest-dom": "^5.16.3",
    "@testing-library/react": "^13.0.0",
    "@testing-library/user-event": "^14.0.4",
    "react": "^18.0.0",
    "react-dom": "^18.0.0",
    "react-scripts": "5.0.0",
    "sass": "^1.49.10",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "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"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我看到了一个有类似问题的 stackoverflow 帖子,并尝试实施那里提到的解决方案,但没有成功。

  1. 起初,我用--force- 重试了这个命令npm i @mui/icons-material @mui/material --force

这使我能够安装 MUI 图标包而不会出现任何错误,但是,当我尝试在项目中使用这些图标时,我遇到了许多错误。

  1. 接下来,我将文件中的reactreact-dom版本更改为 16.8.0 package.json,然后点击npm install。然而,这次我也遇到了以下错误 -
PS D:\REACT Projects\netflix> npm install
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: netflix@0.1.0
npm ERR! Found: react@16.14.0
npm ERR! node_modules/react
npm ERR!   react@"^16.8.0" from the root project
npm ERR!   peer react@">=16.8.0" from @emotion/react@11.8.2
npm ERR!   node_modules/@emotion/react
npm ERR!     peerOptional @emotion/react@"^11.5.0" from @mui/material@5.5.3
npm ERR!     node_modules/@mui/material
npm ERR!       @mui/material@"^5.5.3" from the root project
npm ERR!       1 more (@mui/icons-material)
npm ERR!     peer @emotion/react@"^11.0.0-rc.0" from @emotion/styled@11.8.1
npm ERR!     node_modules/@emotion/styled
npm ERR!       peerOptional @emotion/styled@"^11.3.0" from @mui/material@5.5.3
npm ERR!       node_modules/@mui/material
npm ERR!         @mui/material@"^5.5.3" from the root project
npm ERR!         1 more (@mui/icons-material)
npm ERR!   1 more (@emotion/styled)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^17.0.0" from @mui/material@5.5.3
npm ERR! node_modules/@mui/material
npm ERR!   @mui/material@"^5.5.3" from the root project
npm ERR!   peer @mui/material@"^5.0.0" from @mui/icons-material@5.5.1
npm ERR!   node_modules/@mui/icons-material
npm ERR!     @mui/icons-material@"^5.5.1" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
Run Code Online (Sandbox Code Playgroud)

老实说我现在不知道该怎么办。我认为互联网上会有更好的解决方案,但我到处都看到相同的答案,要么使用 --force/--legacy-peer-deps 要么降级我的react/react-dom版本。如上所述,这些选项都不起作用。对此有更好的解决方案吗?老实说,我很惊讶这样一个众所周知的软件包在安装时会出现这么多问题。

编辑 - 问题已解决
感谢@ckesplin 的帮助,它终于成功了。我按照以下步骤操作 -

  1. 执行npx create-react-app netflix
  2. react版本从 18.0.0更改react-dom17.0.0
  3. 已删除的package-lock.json文件和node_modules文件夹。然后打npm install
  4. 执行npm i @mui/icons-material @mui/material...安装成功。
  5. 然而,在使用图标时,我再次遇到了一堆Module not find: Can'tsolve@emotion/styled错误。所以我不得不npm i @emotion/react @emotion/styled

Car*_*lin 6

日志中显示@mui/material@5.5.3需要react@"^17.0.0". (您尝试同时响应 18.0.0 和 16.14.0,但不响应 ^17.0.0)

替换你的以下内容package.json并给它 ol'npm install

 "dependencies": {
    ...
    "react": "^17.0.0",
    "react-dom": "^17.0.0",
    ...
  },
Run Code Online (Sandbox Code Playgroud)

  • 嗨@ckesplin,谢谢 - 终于成功了。我按照以下步骤操作 - 1.执行“npx create-react-app”。2. 将 `react` 和 `react-dom` 版本从 18.0.0 更改为 17.0.0。3.删除了`package-lock.json`文件和`node_modules`文件夹。然后点击“npm install”。4.执行`npm i @mui/icons-material @mui/material`...安装成功。5. 然而,在使用图标时,我再次收到一堆“找不到模块:无法解析'@emotion/styled”的错误。所以我必须`npm install @emotion/react @emotion/styled`。 (2认同)