创建 React 应用程序时出错,无法启动 React 应用程序

Error occured while creating a react app and unable to start the react app

我是 React 技术的新手,在创建 React 应用程序时遇到错误:

Initialized a git repository.

Installing template dependencies using npm...
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: frontend@0.1.0
npm ERR! Found: react@18.0.0
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/react@12.1.5
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 ERR!
npm ERR! See C:\Users\nahee\AppData\Local\npm-cache\eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\nahee\AppData\Local\npm-cache\_logs22-04-12T12_46_13_743Z-debug-0.log
`npm install --no-audit --save @testing-library/jest-dom@^5.14.1 @testing-library/react@^12.0.0 @testing-library/user-event@^13.2.1 web-vitals@^2.1.0` failed

因此我无法启动应用程序

为此,我使用了 npm uninstall -g create-react-appnpm cache verify 命令来清除错误,即使我无法解决它。

为了创建我使用的 React 应用程序

npx create-react-app frontend

我的问题是什么?

我认为 react-testing-library 有问题。根据错误消息,它仅支持 React < 18 版本。你可以阅读here.

对于替代解决方案,您可以手动将它们添加到您的项目中,就像他们在错误日志中提到的那样(我为自己所做的)

npm install --no-audit --save @testing-library/jest-dom@^5.14.1 @testing-library/react@^12.0.0 @testing-library/user-event@^13.2.1 web-vitals@^2.1.0` failed

或者你也可以检查这个。