首页 > 解决方案 > 尝试使用 react 17 安装 webpack 5 以进行模块联合,在 npm install 上出现此错误

问题描述

我有一个用 cra 创建的反应应用程序。我想安装 webpack 以设置模块联合。

我在运行以下命令时收到此错误:

$ npm i --D webpack@5.24.0 webpack-cli webpack-server html-webpack-plugin babel-loader webpack-dev-server
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: netlify-foot@0.1.0
npm ERR! Found: react@17.0.2
npm ERR! node_modules/react
npm ERR!   react@"^17.0.2" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"~0.9" from webpack-server@0.1.2
npm ERR! node_modules/webpack-server
npm ERR!   dev webpack-server@"*" 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:\AppData\Local\npm-cache\eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\AppData\Local\npm-cache\_logs\2021-06-05T15_25_05_470Z-debug.log

包.json

{
  "dependencies": {
    "@testing-library/jest-dom": "^5.12.0",
    "@testing-library/react": "^11.2.7",
    "@testing-library/user-event": "^12.8.3",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.1.2"
  }
}

问题是什么?如何让这个工作?

标签: reactjswebpackwebpack-dev-server

解决方案


您的安装命令是:

npm i --D webpack@5.24.0 webpack-cli webpack-server html-webpack-plugin babel-loader webpack-dev-server

这意味着,它会尝试添加以下六个包:

  • webpack@5.24.0
  • webpack-cli
  • webpack 服务器
  • html-webpack-插件
  • 通天塔装载机
  • webpack-开发服务器

webpack-server不是一回事。去掉它。

如果您仔细查看错误消息,它会试图告诉您同样的事情:

npm 错误!无法解决依赖关系:

npm 错误!来自 webpack-server@0.1.2 的 peer react@"~0.9"

它说它无法满足由 this 引起的依赖关系webpack-server。确保摆脱它。这至少可以解决这个问题。

你的下一个问题

但是请注意,为了进一步自定义CRA's webpack 构建,如果您不想这样做eject,您还需要一种方法来覆盖它的默认构建设置。

为此,您可能需要查看CRACO.

CRACO(如 Webpack Module Federation)没有很好的文档,但这里有一些与 webpack 相关的示例


推荐阅读