由于与 babel 的依赖关系而安装 React 时出现问题

问题描述 投票:0回答:2

基本上就是这样。我都试过了

npm init react-app my-app

npx create-react-app my-app

我总是收到这样的消息:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! Found: @babel/[email protected]
npm ERR! node_modules/@babel/core
npm ERR!   @babel/core@"7.12.3" from [email protected]
npm ERR!   node_modules/react-scripts
npm ERR!     react-scripts@"*" from the root project
npm ERR!   @babel/core@"^7.12.3" from @svgr/[email protected]
npm ERR!   node_modules/@svgr/webpack
npm ERR!     @svgr/webpack@"5.5.0" from [email protected]
npm ERR!     node_modules/react-scripts
npm ERR!       react-scripts@"*" from the root project
npm ERR!   9 more (babel-jest, babel-loader, ...)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @babel/core@"^7.13.0" from @babel/[email protected]
npm ERR! node_modules/@babel/preset-env/node_modules/@babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining
npm ERR!   @babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining@"^7.13.12" from @babel/[email protected]
npm ERR!   node_modules/@babel/preset-env
npm ERR!     @babel/preset-env@"^7.12.1" from @svgr/[email protected]
npm ERR!     node_modules/@svgr/webpack
npm ERR!       @svgr/webpack@"5.5.0" from [email protected]
npm ERR!       node_modules/react-scripts
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\Zio Matteo\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\Zio Matteo\AppData\Local\npm-cache\_logs\2021-06-08T08_46_00_758Z-debug.log

Aborting installation.
  npm install --save --save-exact --loglevel error react react-dom react-scripts cra-template has failed.

Deleting generated file... package.json
Deleting first/ from C:\Users\Zio Matteo\Desktop\react
Done.
npm ERR! code 1
npm ERR! path C:\Users\Zio Matteo\Desktop\react
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c create-react-app "first"

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Zio Matteo\AppData\Local\npm-cache\_logs\2021-06-08T08_46_01_359Z-debug.log

我也更新了节点,但没有任何效果。我的 package.json 是空的,因为这是我尝试安装的第一个包,所以我也没有 node_modules 文件夹。我能做什么?

node.js reactjs npm terminal create-react-app
2个回答
9
投票

这是npm版本的问题。升级到npm最新版本:

npm install -g npm@latest

还添加babel:

npm install --save-dev @babel/core

如果此解决方案不起作用,您可以尝试旧的 npm 版本:

npm install [email protected] -g

0
投票

节点包管理器 CLI(例如 npm、yarn 和 pnpm)使工程师能够通过将覆盖或解析对象添加到存储库的根 package.json 来解决依赖项特异性问题。这对于解决安全性和/或代码的依赖性问题非常有用。

npm:https://docs.npmjs.com/cli/v8/configuring-npm/package-json#overrides

纱线:https://yarnpkg.com/configuration/manifest#resolutions

© www.soinside.com 2019 - 2024. All rights reserved.