Fail to create React project due to wrong webpack version

Viewed 220

When I create React project, it has below error report:

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

  "webpack": "4.44.2"

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:

  /Users/lizhiyuan/node_modules/webpack (version: 4.46.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.

I go to the package.json file, but I did not find related codes in it. The part code of the file is:

"dependencies": {
    "@testing-library/jest-dom": "^5.14.1",
    "@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"
  },

In addition, another tip is:

If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.

However, I cannot find such ".env" file in my project. It is also hard to create ".env" file on my MacBook because the behavior is deterred by the system.

Does anyone know how to fix it?

1 Answers

Yes, I've heard CRA has issues with webpack. I'd suggest you to write your own project from scratch with a custom webpack 5 config.

However, you can simply fix this issue by creating a file named .env at the root of your project and adding this line in it: SKIP_PREFLIGHT_CHECK=true. Restart you app, it will work. MaxOs doesn't hamper you to create .env file in your IDE (vscode for ex.).

Related