node.js - 添加 webpack 时 react 项目依赖错误

标签 node.js reactjs npm webpack

我已经用下面的命令安装了 webpack

yarn add webpack --dev

在我的 React 应用程序文件夹中运行 yarn start 命令之后

D:\Workspace\fuel-man-ui\fuel-man-ui>yarn start

低于错误。

yarn run v1.19.0 $ react-scripts start

There might be a problem with the project dependency tree. It is likely not a bug in Create React App, but something you need to fix locally.

The react-scripts package provided by Create React App requires a dependency:

"webpack": "4.29.6"

Don't try to install it manually: your package manager does it automatically. However, a different version of webpack was detected higher up in the tree:

D:\Workspace\fuel-man-ui\fuel-man-ui\node_modules\webpack (version: 4.41.0)

Manually installing incompatible versions is known to cause hard-to-debug issues.

If you would prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project. That will permanently disable this message but you might encounter other issues.

To fix the dependency tree, try following the steps below in the exact order:

  1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
  2. Delete node_modules in your project folder.
  3. Remove "webpack" from dependencies and/or devDependencies in the package.json file in your project folder.
  4. Run npm install or yarn, depending on the package manager you use.

In most cases, this should be enough to fix the problem. If this has not helped, there are a few other things you can try:

  1. If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead. This may help because npm has known issues with package hoisting which may get resolved in future versions.

  2. Check if D:\Workspace\fuel-man-ui\fuel-man-ui\node_modules\webpack is outside your project directory. For example, you might have accidentally installed something in your home folder.

  3. Try running npm ls webpack in your project folder. This will tell you which other package (apart from the expected react-scripts) installed webpack.

If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project. That would permanently disable this preflight check in case you want to proceed anyway.

P.S. We know this message is long but please read the steps above :-) We hope you find them helpful!

error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

i have deleted node_modules from my project and devdependencies, also i have removed global node_modules and cache. still getting same error.

我运行了 npm ls webpack 并到达了树下

fuel-man-ui@0.1.0 D:\Workspace\fuel-man-ui\fuel-man-ui
+-- react-scripts@3.0.1
| `-- webpack@4.29.6
|   `-- webpack@4.41.0  extraneous
`-- webpack@4.41.0

npm ERR! extraneous: webpack@4.41.0 D:\Workspace\fuel-man-ui\fuel-man-ui\node_modules\react-scripts\node_modules\webpack\node_modules\webpack

最佳答案

The react-scripts package provided by Create React App requires a dependency:

"webpack": "4.29.6"

Don't try to install it manually: your package manager does it automatically. However, a different version of webpack was detected higher up in the tree:

在项目根目录中删除 node_modules 和 yarn.lock。在 package.json 中从依赖项中删除 webpack 并再次运行 yarn。

如果你想为这个项目修改 webpack.config 你可以运行 yarn eject从这里开始,您将负责 webpack.config,因此更新 React 脚本将更加困难。

如果你不想对 webpack.config 负责,你可以尝试使用 react-app-rewired关于此的文章是 here

关于node.js - 添加 webpack 时 react 项目依赖错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58233418/

相关文章:

javascript - 如何设置连接多方上传文件的名称?

javascript - 如何将视频(从 getUserMedia)发送到 Node.js 服务器?

javascript - 在 React 中,如何通过编写一个不导入任何文件的简单函数来添加复制到剪贴板功能?

reactjs - 如何增加 react-native Slider 组件的高度?

node.js - react : NPM install fails

npm:如何在 Windows (10) 中设置 NODE_ENV?

node.js - 如何在 MongoDB 中创建动态查询?

node.js - 使用 Mongo/Express/Angular/Node 推送到模型中的数组

javascript - 在 React 中获取 Click 的输入值

npm - 强制 npm 更新依赖