使用npx create-react-app创建项目遇ERESOLVE及ENOENT错误求助
React项目创建中的两个常见错误解决
一、ERESOLVE依赖树冲突错误
执行npx create-react-app calc-app时出现如下错误:
Installing template dependencies using npm... npm error code ERESOLVE npm error ERESOLVE unable to resolve dependency tree npm error npm error While resolving: calc-app@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. npm error npm error npm error For a full report see: npm error C:\Users\ariana\AppData\Local\npm-cache\_logs\2024-12-18T14_26_32_953Z-eresolve-report.txt npm error A complete log of this run can be found in: C:\Users\ariana\AppData\Local\npm-cache\_logs\2024-12-18T14_26_32_953Z-debug-0.log `npm install --no-audit --save @testing-library/jest-dom@^5.14.1 @testing-library/react@^13.0.0 @testing-library/user-event@^13.2.1 web-vitals@^2.1.0` failed
解决方法:
方法1:使用
--legacy-peer-deps参数创建项目
npm 7及以上版本默认启用严格的peer依赖检查,添加该参数可恢复旧版npm的依赖解析逻辑,绕过冲突:npx create-react-app calc-app --legacy-peer-deps方法2:手动指定兼容的React版本
若不想使用旧版依赖解析,可指定React 18版本创建项目(@testing-library/react 13兼容React 18):npx create-react-app calc-app --use-npm # 创建完成后进入项目目录,执行以下命令降级React版本 npm install react@^18.2.0 react-dom@^18.2.0
二、ENOENT找不到package.json错误
执行npm install时出现如下错误:
npm error code ENOENT npm error syscall open npm error path C:\Users\ariana\Documents\Cursos\React\package.json npm error errno -4058 npm error enoent Could not read package.json: Error: ENOENT: no such file or directory, open 'C:\Users\ariana\Documents\Cursos\React\package.json' npm error enoent This is related to npm not being able to find a file. npm error enoent npm error A complete log of this run can be found in: C:\Users\ariana\AppData\Local\npm-cache\_logs\2024-12-18T14_30_41_989Z-debug-0.log
解决方法:
错误原因是当前终端目录并非项目根目录(calc-app),而是上级的React文件夹,该目录下没有package.json。执行以下步骤:
- 进入项目根目录:
cd C:\Users\ariana\Documents\Cursos\React\calc-app - 再执行
npm install即可。
内容的提问来源于stack exchange,提问作者Ari Di Matteo
相关产品推荐
相关产品推荐

