首先,贴一下我的错误,在使用create-react-app创建一个项目之后,我用vscode打开这个项目,并且在终端输入命令npm start之后,出现报错的情况:
C:\Users\Lenovo\Desktop\react-demo
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:
C:\Users\23902\node_modules\webpack (version: 4.30.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 C:\Users\23902\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!
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! jianshu@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the jianshu@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\23902\AppData\Roaming\npm-cache\_logs\2019-05-27T13_41_56_542Z-debug.log
出现这个错误网上有很多种解决方法,屡试不爽,很多方法用了都还是没有解决这个问题,之后我试了两种解决方法都可以完美解决这个问题
第一种方法就是把你项目中版本不同的插件都升级成项目相同版本
怎么升级?
进到package.json文件中找到
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.
这里所提示的 webpack ,然后package.json中的webpack版本手动改成上面提示的版本 再重新 yarn 一下,就解决了 如果在 package.json 中找不到webpak的话可以先执行 yarn eject 命令,然后在package.json 就能看到项目所有依赖项啦
第二种解决方法
第二种相较第一种更简单一些
不需要频繁的去 yarn
在项目的根目录中 新建一个 .env 文件
文件中只需要写进 SKIP_PREFLIGHT_CHECK=true 这句代码 就完美解决啦 这段代码的意思就是 忽略项目版本差异!