使用Create React App创建项目遇ERESOLVE依赖解析错误求助
解决create-react-app依赖树冲突问题
问题根源
create-react-app当前默认会安装React 19,但配套的@testing-library/react 13.4.0仅支持React 18.x版本,peer依赖版本不匹配触发了ERESOLVE报错。你之前执行的npx uninstall react react-dom无效,一是因为命令语法错误(卸载依赖需在项目目录内用npm uninstall,而非npx),二是没从根源解决版本不兼容问题。
解决方案
方案1:创建React 18兼容的项目
直接指定适配React 18的模板版本初始化项目,彻底避开版本冲突:
# 基础JS项目 npx create-react-app my-app --template cra-template@5.0.1 # TypeScript项目 npx create-react-app my-app --template @cra-template/pwa-typescript@5.0.1
这些模板版本会锁定React 18.x,与@testing-library/react 13.x的peer依赖要求完全匹配。
方案2:升级测试库适配React 19
如果想使用React 19,按以下步骤操作:
- 强制初始化项目(忽略依赖冲突报错):
npx create-react-app my-app --force
- 进入项目目录:
cd my-app
- 升级@testing-library/react到支持React 19的最新版本(14.x及以上已适配):
npm install @testing-library/react@latest
方案3:用npm overrides强制兼容
通过npm的依赖覆盖功能,让@testing-library/react接受React 19版本:
- 强制初始化项目:
npx create-react-app my-app --force
- 在项目根目录的
package.json中添加以下配置:
"overrides": { "@testing-library/react": { "react": "^19.0.0" } }
- 重新安装依赖:
npm install
额外提示
- Node.js 22属于较新的版本,create-react-app对其兼容性仍在优化中,若遇到其他异常,可临时切换到Node.js 20(LTS长期支持版本),用nvm等版本管理工具切换更便捷。
内容的提问来源于stack exchange,提问作者Gamerz
相关产品推荐
相关产品推荐

