我正在创建 React 项目并使用 Mantine 库。当我尝试安装 @modulz/radix-icons 依赖项时,我收到错误消息,指出存在依赖项冲突。我该如何解决这个问题?
错误
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/react
npm ERR! react@"^18.2.0" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.x || ^17.x" from @modulz/[email protected]
npm ERR! node_modules/@modulz/radix-icons
npm ERR! @modulz/radix-icons@"*" 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\igorg\AppData\Local\npm-cache\eresolve-report.txt for a full report.
最新版本的 @modulz/radix-icons 包具有以下语义版本的对等依赖关系。
$ npm view @modulz/radix-icons@latest peerDependencies
{ react: '^16.x || ^17.x' }
如您所见,它与
React ^16.x
和 React ^17.x
兼容。不支持 React 18.x
,您的项目中安装的 react
版本是 18.2.0
,这就是您收到此警告的原因。
该软件包的最后一次发布是在 2 年前。因此,唯一安全的方法是将
react
软件包降级到版本 16.x
或 17.x
。检查17.x
的最新版本:
$ npm view react@17 version
[email protected] '17.0.0'
[email protected] '17.0.1'
[email protected] '17.0.2'
安装它们:
$ npm i react@^17.0.2 -S
$ npm i @modulz/radix-icons -S
added 1 package, and audited 6 packages in 3s
found 0 vulnerabilities
警告消失了。
注意:请小心使用
pm install <package>
或 --force
选项运行 --legacy-peer-deps
,否则可能会导致损坏。相反,我们应该修复依赖包之间的版本兼容性问题。
使用
npm install --legacy-peer-deps
一个简单的替代方法是卸载所有您使用的 mantine 库,然后重新安装它们。这将一次性安装正确的版本,您无需担心
--force
或--legacy-peer-deps
导致的错误。
示例:
卸载:
npm uninstall @mantine/core @mantine/hooks @mantine/form @mantine/dates dayjs @mantine/notifications @mantine/modals
并重新安装:
npm install @mantine/core @mantine/hooks @mantine/form @mantine/dates dayjs @mantine/notifications @mantine/modals
您可以选择您使用的库并从此处复制命令:https://mantine.dev/getting-started/#get-started-without-framework。记得将命令从“安装”更改为“卸载”