在创建react 创建一个本地项目的时候
npx create-react-app my-react-app
然后npm 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.42.0"
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/cyli/node_modules/webpack (version: 3.12.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:
5. 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.
6. Check if /Users/cyli/node_modules/webpack is outside your project directory.
For example, you might have accidentally installed something in your home folder.
7. 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!
这个报错是你在全局安装了webpack ,需要卸载全局的webpack
删除全局webpack
npm uninstall -g web-pack
# 为什么要局部全局删除webpack
# 因为你可能在安装webpack时不确定自己是全局安装
# 还是本地安装,所以建议先执行全局删除命令
# 然后在执行下面的本地删除命令
删除本地webpack
npm un webpack
检查webpack残余文件
ls
# 用ls命令查看一下是否有这几个文件
# node_modules
# package-lock.json
# package.json
# 有是最好的,如果没有那你可能还没找到自己本地安装webpack的准确位置
# 若有,执行下面的方法
rm -rf node_modules package-lock.json package.json
# 上面这行命令是删除这些文件的意思
# 你的webpack 彻底删除干净了
正确安装webpack4.+
创建一个新的本地项目目录名为webpack-demo
mkdir webpack-demo
进入目录
cd webpack-demo
创建package.json文件
npm init -y
安装webpack
# 要安装最新版本或特定版本,请运行以下命令之一:
# 第一个安装方式默认安装最新版本
npm install --save-dev webpack
# 第二个安装方式是安装你需要的版本
npm install --save-dev webpack@<version>
安装webpack-cli
npm install --save-dev webpack-cli
# 不要忘记webpack4.+开始webpack-cli是必备的
检查webpack是否安装成功
node_modules/.bin/webpack -v
# 执行了这个命令以后会输出一个版本号