无法使用 create react app 创建 React 项目

Unable to create React project using create react app

所以我正在尝试学习 React,按照 https://reactjs.org/docs/create-a-new-react-app.html 上的说明进行操作。具体来说,我正在使用 Create React App,碰巧遇到错误:

Installing template dependencies using npm...
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: test-react@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!

据我所见,测试库要求安装的 React 版本低于 18.0.0,但 Create React App 只是自动安装最新的 React 版本,导致错误。

我该如何解决这个问题?如前所述,我是一个完全的新手,所以非常感谢任何帮助。

在暂时解决此问题之前,您可以删除 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