React 18 的 create-react-app 依赖版本问题

2024-05-02

npx create-react-app my-project导致以下依赖错误:

npx版本:8.5.0

Installing template dependencies using npm...
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: [email protected] /cdn-cgi/l/email-protection
npm ERR! Found: [email protected] /cdn-cgi/l/email-protection
npm ERR! node_modules/react
npm ERR!   react@"^18.0.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"<18.0.0" from @testing-library/[email protected] /cdn-cgi/l/email-protection
npm ERR! node_modules/@testing-library/react
npm ERR!   @testing-library/react@"^12.0.0" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

该命令仍然会生成一个项目目录,但运行npm start在创建的目录中出现错误web-vitals失踪于node-modules.

尝试过的解决方案

  • 运行相同的命令--force or --legacy-peer-deps按照上述错误消息的建议并不能解决问题。

  • 正在删除node_modules and package-lock.json和跑步npm i也没有解决问题。

Update

该问题已通过最新更新修复create-react-app。现在它创建一个项目没有任何问题。


在这个问题暂时解决之前,您可以删除node_modules文件夹和package-lock.json。接下来,打开package.json并改变
"react": "^18.0.0" & "react-dom": "^18.0.0"到早期版本,例如:
"react": "^17.0.2" & "react-dom": "^17.0.2".
最后,你可以运行npm install.

替代解决方案(首先尝试这个!):
建议的解决方案朱尼1998 https://github.com/facebook/create-react-app/issues/12269#issuecomment-1095746716):

  1. 删除两者node_modules and package-lock.json
  2. run npm i web-vitals --save-dev
  3. run npm install

然后你可以使用npm run build and npm start again

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React 18 的 create-react-app 依赖版本问题 的相关文章

随机推荐