react-native - 升级到 npm7 但库中断太多

一直在使用 6.14.7 的 NPM 版本,并决定为我的 react-native 项目继续使用 7.20.1 的 NPM 版本。当我尝试 npm install 时,我的大多数库都因 Could not resolve dependency:

错误而中断

示例

// example 1
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.8" from @react-native-community/async-storage@1.12.1

// example 2
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.0" from @react-native-community/picker@1.8.1

// More Example(s)
..
...
.....

仅供引用,我知道 breaking change of peer dependencies announcement由 NPM 团队制作。此处需要一些指导,如何无缝升级我的 npm 版本?

Automatically installing peer dependencies is an exciting new feature introduced in npm 7. In previous versions of npm (4-6), peer dependencies conflicts presented a warning that versions were not compatible, but would still install dependencies without an error. npm 7 will block installations if an upstream dependency conflict is present that cannot be automatically resolved.

当然,我知道以下选项。如果我要执行 npm install --legacy-peer-deps,这不是和我使用 NPM 6 一样吗?因为它只是通过传递自动安装对等依赖项。

You have the option to retry with --force to bypass the conflict or --legacy-peer-deps command to ignore peer dependencies entirely (this behavior is similar to versions 4-6).

决定

只是分享我的想法,希望能帮助到其他人。

在通过 NPM7 阅读更多内容(link1、link2)以及 @Dan Macák@M 分享的评论之后。 Erim Tuzcuoglu,我决定暂时留在 NPM6。

主要是我认为升级到 NPM7 所付出的努力和风险远远高于返回?无论如何,我不需要 NPM7 中提供的新功能?

我也尝试过使用 @M 建议的 NVM。 Erim Tuzcuoglu,然后使用不同的 NVM + NPM 进行切换,最终从 xcode 中遇到 1 个问题(Command PhaseScriptExecution 失败,退出代码非零)。我认为原因是 nvm 与“PREFIX”不兼容 Many has faced this issue并提出了一些解决方案,但我决定不再进一步,因为这似乎会使项目更加复杂(其他团队成员需要对 NVM 进行相同的设置)

最佳答案

恐怕您的项目没有简单的方法可以切换到 npm 7。NPM 决定通过安装 peerDependencies 让我们的生活更轻松(考虑到您的问题有点讽刺)。自动,涉及处理无效的依赖树;虽然这是一件好事,可以防止难以发现的错误(例如,由于插件期望的 React 版本与您在项目中使用的版本不同,因此失败),以防万一忽略安装警告(谁不有时会想念他们吗?),切换可能会很痛苦。

因此,虽然 NPM 应该自己处理许多依赖差异(来自 https://github.blog/2021-02-02-npm-7-is-now-generally-available/):

Since many packages in the ecosystem have come to rely on loose peer dependencies resolutions, npm 7 will print a warning and work around most peer conflicts that exist deep within the package tree, since you can’t fix those anyway.

如果无法解决您的依赖关系,您仍然需要采取手动操作。如果您仍想使用 npm 7,这里有几个选项:

  1. 您以不破坏对等依赖要求的方式更新您的依赖关系。因此,在您的情况下,您更新依赖项以使用和期望相同范围的 React 版本。这个选项当然很乏味,需要重新测试受影响的代码。但它处理了因依赖树损坏而导致的潜在问题。所以如果运行 npm ls没有成功运行,你应该考虑一下这个选项。不幸的是,对于某些 peerDependencies 非常窄的包来说可能会很困难。要求,库作者在指定时应该非常小心
  2. 使用--legacy-peer-depsnpm i 期间忽略所有 peerDependencies .

FWIW,如果您没有时间处理前者并且不想因为后者而改变您的工作流程,并且真的不需要 npm 7 的更改,那么暂时使用版本 6 是完全可以的.

https://stackoverflow.com/questions/68519568/

相关文章:

amazon-s3 - 从 Lambda 函数生成预签名 S3 URL 时为 "Access key

vue.js - 如何配置 Vite 来解析嵌套的 SFC CSS url

python - 使用 gunicorn 和 Nginx 部署时,Django 应用程序未在生产日志

javascript - Cypress :找不到模块:错误:无法解析 'cypress-wait-

c# - 无法将 Dictionary> 转换为 IRea

android - 尝试在真实手机上启动应用程序时,Expo 卡在无休止的加载屏幕上

html - 如何强制 Chrome 在网站上显示最新的图像更改

c++ - 什么是 list 文件?

rust - 为什么 Iterator::filter 方法接受一个可变引用作为 self?

reactjs - React Signalr 访问状态变量