npm 安装因对等依赖冲突而失败:react@16.14.0

npm install fails with Conflicting peer dependency: react@16.14.0

尝试为 this 项目安装依赖项时,npm install 失败并显示

> npm install
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR! 
npm ERR! While resolving: ecommerce-app@0.1.0
npm ERR! Found: react@17.0.2
npm ERR! node_modules/react
npm ERR!   react@"latest" from the root project
npm ERR!   peer react@"^16.8.0 || ^17.0.0" from @material-ui/core@4.11.4
npm ERR!   node_modules/@material-ui/core
npm ERR!     @material-ui/core@"latest" from the root project
npm ERR!     peer @material-ui/core@"^4.0.0" from @material-ui/icons@4.11.2
npm ERR!     node_modules/@material-ui/icons
npm ERR!       @material-ui/icons@"latest" from the root project
npm ERR!     1 more (@material-ui/lab)
npm ERR!   10 more (react-dom, @material-ui/icons, @material-ui/lab, ...)
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! redux-form@"^8.3.6" from the root project
npm ERR! 
npm ERR! Conflicting peer dependency: react@16.14.0
npm ERR! node_modules/react
npm ERR!   peer react@"^16.4.2" from redux-form@8.3.7
npm ERR!   node_modules/redux-form
npm ERR!     redux-form@"^8.3.6" 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 /Users/gergo/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/gergo/.npm/_logs/2022-03-17T17_37_09_399Z-debug-0.log

编辑

当 运行 npm install --force 时,我遇到了另一个错误

npm ERR! 2 warnings generated.
npm ERR! In file included from ../src/libsass/src/values.cpp:3:
npm ERR! In file included from ../src/libsass/src/values.hpp:4:
npm ERR! ../src/libsass/src/ast.hpp:1614:25: warning: loop variable 'numerator' of type 'const std::__1::basic_string<char>' creates a copy from type 'const std::__1::basic_string<char>' [-Wrange-loop-analysis]
npm ERR!         for (const auto numerator : numerators)
npm ERR!                         ^
npm ERR! ../src/libsass/src/ast.hpp:1614:14: note: use reference type 'const std::__1::basic_string<char> &' to prevent copying
npm ERR!         for (const auto numerator : numerators)
npm ERR!              ^~~~~~~~~~~~~~~~~~~~~~
npm ERR!                         &
npm ERR! ../src/libsass/src/ast.hpp:1616:25: warning: loop variable 'denominator' of type 'const std::__1::basic_string<char>' creates a copy from type 'const std::__1::basic_string<char>' [-Wrange-loop-analysis]
npm ERR!         for (const auto denominator : denominators)
npm ERR!                         ^
npm ERR! ../src/libsass/src/ast.hpp:1616:14: note: use reference type 'const std::__1::basic_string<char> &' to prevent copying
npm ERR!         for (const auto denominator : denominators)
npm ERR!              ^~~~~~~~~~~~~~~~~~~~~~~~
npm ERR!                         &
npm ERR! 2 warnings generated.
npm ERR! In file included from ../src/binding.cpp:1:
npm ERR! In file included from ../../nan/nan.h:56:
npm ERR! In file included from /Users/gergo/.node-gyp/17.4.0/include/node/node.h:63:
npm ERR! In file included from /Users/gergo/.node-gyp/17.4.0/include/node/v8.h:25:
npm ERR! In file included from /Users/gergo/.node-gyp/17.4.0/include/node/v8-array-buffer.h:12:
npm ERR! In file included from /Users/gergo/.node-gyp/17.4.0/include/node/v8-local-handle.h:12:
npm ERR! /Users/gergo/.node-gyp/17.4.0/include/node/v8-internal.h:563:38: error: no template named 'remove_cv_t' in namespace 'std'; did you mean 'remove_cv'?
npm ERR!             !std::is_same<Data, std::remove_cv_t<T>>::value>::Perform(data);
npm ERR!                                 ~~~~~^~~~~~~~~~~
npm ERR!                                      remove_cv
npm ERR! /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/type_traits:697:50: note: 'remove_cv' declared here
npm ERR! template <class _Tp> struct _LIBCPP_TEMPLATE_VIS remove_cv
npm ERR!                                                  ^
npm ERR! 1 error generated.
npm ERR! make: *** [Release/obj.target/binding/src/binding.o] Error 1
npm ERR! gyp ERR! build error 
npm ERR! gyp ERR! stack Error: `make` failed with exit code: 2
npm ERR! gyp ERR! stack     at ChildProcess.onExit (/Users/gergo/Documents/spring-boot-react-ecommerce-app/client/node_modules/node-gyp/lib/build.js:262:23)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:520:28)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12)
npm ERR! gyp ERR! System Darwin 19.6.0
npm ERR! gyp ERR! command "/usr/local/Cellar/node/17.4.0/bin/node" "/Users/gergo/Documents/spring-boot-react-ecommerce-app/client/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
npm ERR! gyp ERR! cwd /Users/gergo/Documents/spring-boot-react-ecommerce-app/client/node_modules/node-sass
npm ERR! gyp ERR! node -v v17.4.0
npm ERR! gyp ERR! node-gyp -v v3.8.0
npm ERR! gyp ERR! not ok 
npm ERR! Build failed with error code: 1

编辑

安装 node-sass 时抛出第二个错误。我的节点版本与“node-sass”不兼容:“^4.14.1”。我已将 node-sass 版本更改为与我的节点版本兼容,此错误消失了。并且构建成功

像这样安装依赖项:

npm install <dependencyname> --force

或者如果您想一次安装所有依赖项,

npm install --force

还是失败了?对我来说,使用节点版本 14.16.0

npm install 可以正常工作

安装 node-sass 时出现第二个错误。 我的节点版本 (17.4) 与 "node-sass": "^4.14.1"

不兼容

我已更改 node-sass 版本以与我的节点版本兼容,此错误消失了。并且构建成功。

您需要升级 node-sass 版本以与 Node.js 的当前 LTS 版本兼容。

为此,请更改您的 package.json 文件以包含以下内容(替换旧声明)。

{
  "dependencies": {
    "node-sass": "^7.0.1"
  }
}

然后,运行下面的命令,应该没有错误。

$ npm install

忽略$,它只是表示终端。

由于 @material-ui/core 已弃用,您会收到错误消息,请参阅:https://www.npmjs.com/package/@material-ui/core material-ui 的新包是 https://www.npmjs.com/package/@mui/material.

正如他们在这里所说的那样 https://mui.com/guides/migration-v4/#update-react-amp-typescript-version

  • React 的最低支持版本从 v16.8.0 增加到 v17.0.0。

您应该考虑升级您的 React 和 material ui,他们还将样式工具更改为情感,您可以在上面的 link 中找到更多信息。 此外,对于像你这样的情况,他们有很好的迁移 guides,检查 https://mui.com/components/data-grid/migration-v4/#migrating-mui-core-from-v4

临时替代方法可以使用强制安装(npm install --force),但我真的建议升级您的依赖项,否则无法保证一切正常。

尝试将包管理更改为 yarn。目前大部分包已经支持yarn,其依赖处理也比较好

yarn install

对于第一个错误,我尝试在终端中通过以下 2 个命令依次安装 npm。它为我修复:

npm install --legacy-peer-deps
npm audit fix --force`