【问题标题】:Issue with Material UI Icons npm installation : unable to resolve dependency treeMaterial UI Icons npm 安装问题:无法解析依赖关系树
【发布时间】:2022-11-05 16:26:18
【问题描述】:

---------问题已解决 - 问题结束时的解决方案--------

我试图使用 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.

以下是我的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"
    ]
  }
}

我看到了stackoverflow post with similar issue,并尝试实施那里提到的解决方案,但没有成功。

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

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

    1. 接下来,我将package.json 文件中的reactreact-dom 版本更改为16.8.0,然后点击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
    

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

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

    1. 已执行npx create-react-app netflix
    2. reactreact-dom 版本更改为17.0.0从 18.0.0.
    3. 已删除package-lock.json 文件和node_modules 文件夹。 然后点击npm install
    4. 执行npm i @mui/icons-material @mui/material...安装成功。
    5. 但是,在使用图标时,我又遇到了一堆错误未找到模块:无法解析 @emotion/styled.所以我不得不npm i @emotion/react @emotion/styled

【问题讨论】:

    标签: reactjs npm material-ui dependencies


    【解决方案1】:

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

    替换您的package.json 中的以下内容,并给它npm install

     "dependencies": {
        ...
        "react": "^17.0.0",
        "react-dom": "^17.0.0",
        ...
      },
    

    【讨论】:

    • 嗨@ckesplin,实际上我后来也尝试过 17.0.0。我得到了相同的对等依赖错误。
    • 您是否删除了节点模块文件夹并重新安装了依赖项?
    • 嗨@ckesplin,谢谢 - 终于成功了。我按照以下步骤操作 - 1. 执行 npx create-react-app。 2. 将reactreact-dom 版本从18.0.0 更改为17.0.0。 3、删除package-lock.json文件和node_modules文件夹。然后点击npm install。 4.执行npm i @mui/icons-material @mui/material...安装成功。 5. 但是,在使用图标时,Module not found: Can't resolve '@emotion/styled 再次出现一堆错误。所以我不得不npm install @emotion/react @emotion/styled
    【解决方案2】:

    使用 --force 尝试该命令。 npm i @mui/icons-material @mui/material --force.这对我有用。

    【讨论】:

    • 嗨@Aditya,我实际上是这样做的,并且安装成功。但是,当我尝试在我的项目中使用 MUI 图标时,我遇到了几个与对等依赖相关的错误。不过,我现在已经解决了这个问题。如果你愿意,你可以查看this answer 来了解我做了什么。
    【解决方案3】:

    这也发生在我身上,您可以在命令的最后使用 --force 安装它。

    npm i material-ui --force
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-25
      • 2021-06-14
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 2021-05-05
      • 2023-02-08
      相关资源
      最近更新 更多