webpack 版本过高引起 初试npx create-react-app hello-react报错

创建项目并启动

npm install -g create-react-app

create-react-app hello-react

cd hello-react

cnpm start

然后成功报错:

BYs-MacBook-Pro:hello-react wangbing$ cnpm start

> hello-react@0.1.0 start /Users/wangbing/WebstormProjects/hello-react
> 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.41.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/wangbing/node_modules/webpack (version: 4.41.1) 

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/wangbing/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! hello-react@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the hello-react@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!     /Users/wangbing/.npm/_logs/2019-10-30T02_21_34_096Z-debug.log

问题原因react项目依赖webpack版本为4.19.1,而我安装的是4.41.1,所以导致失败

大多数解决办事是删除全局和本地的 webpack-cliwebpack

解决问题思路:

  • 创建一个 .env 文件
  • 添加这句代码:SKIP_PREFLIGHT_CHECK=true
SKIP_PREFLIGHT_CHECK=true 
  • 再npm start
npx create-react-app和使用Vite创建React应用都是常见的前端工具,它们各有优缺点。 优点: npx create-react-app: - 简单易用:create-react-appReact官方提供的脚手架工具,使用简单,适合快速搭建React项目。 - 完善的生态系统:create-react-app集成了许多常用的工具和配置,如Babel、Webpack等,使得开发者无需手动配置即可进行开发。 - 社区支持:由于create-react-app广泛使用,因此有丰富的社区支持和文档资料可供参考。 Vite: - 极速开发:Vite使用ES模块的开发服务器和即时编译机制,启动速度快,冷启动时间短,能够提供更快的开发体验。 - 原生ES模块支持:Vite利用浏览器的原生ES模块支持,无需打包和转换,能够直接在浏览器中运行和调试代码,提高开发效率。 - 插件生态系统:Vite支持丰富的插件系统,可以轻松集成各种功能和工具,如TypeScript、CSS预处理器等。 缺点: npx create-react-app: - 一体化配置:create-react-app的配置相对固定,对于一些高度定制化的需求可能不够灵活,需要手动eject来获得更多配置项的控制权。 - 较大的初始包体积:create-react-app默认集成了许多工具和库,导致初始包体积较大,对于需要快速加载的项目可能不太合适。 Vite: - 生态系统相对较新:相比create-react-app,Vite的生态系统相对较新,可能在某些功能或插件的支持上不如create-react-app成熟。 - 配置复杂性:Vite相对于传统的打包工具(如Webpack)来说,配置相对简单,但对于不熟悉ES模块化和Vite特有的配置项的开发者来说,上手可能需要一些时间。 综上所述,npx create-react-app适合快速搭建React项目,并且拥有完善的生态系统和社区支持。Vite适合追求极速开发体验、原生ES模块支持和插件生态系统的开发者。选择使用哪种工具取决于项目需求、开发经验和个人偏好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值