【解决 npx create-react-app my-app --template typescript 创建react项目报错】

问题描述

最近react更新到19,使用npx create-react-app my-app --template typescript创建react项目时,由于默认用的是react19,会报错:

Installing template dependencies using npm...
npm error code ERESOLVE
npm error ERESOLVE unable to resolve dependency tree
npm error
npm error While resolving: my-app-react@0.1.0
npm error Found: react@19.0.0
npm error node_modules/react
npm error react@"^19.0.0" from the root project
npm error
npm error Could not resolve dependency:
npm error peer react@"^18.0.0" from @testing-library/react@13.4.0
npm error node_modules/@testing-library/react
npm error @testing-library/react@"^13.0.0" from the root project
npm error
npm error Fix the upstream dependency conflict, or retry
npm error this command with --force or --legacy-peer-deps
npm error to accept an incorrect (and potentially broken) dependency resolution.

有些教程是手动回退到react18版本,但是实测还是不行(目前对我来说)。


解决方案:

不再使用npx create-react-app client --template typescript
使用vite构建工具创建react项目,从而解决依赖冲突问题。
执行完npm create vite@latest my-app --template react后,会有相关手动选项,↑ ↓选择React,再选择TypeScript。
构建完成后,再进行后续操作,完整流程:

npm create vite@latest my-app --template react
//然后选择React,TypeScript
//后续操作
cd my-app
npm install
npm run dev
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值