微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有 Nextjs 和 React-native 的 Monorepo 中的 Nohoist:无法解决 react

如何解决带有 Nextjs 和 React-native 的 Monorepo 中的 Nohoist:无法解决 react

我正在处理的项目由一个 monorepo(带纱线)组成,其中包含一个 react-native 应用程序(特别是 expo)和一个 nestJS 服务器。今天我需要将 nestJS 与 NextJS 集成,并遇到错误“Hooks can only be called inside the body of a function component”。这是因为服务器(使用 NextJs)和移动应用程序(使用 React-Native)都需要 react 模块,但版本不同。 我通过在 monorepo 根目录下的 packages.json 文件添加 nohosit 选项解决了这个问题:

"nohoist": [
    "**/react","**/react/**","**/react-dom","**/react-dom/**"
  ]

然而,尽管这解决了服务器(nestJS 和 NextJS)的问题,但它使用无法找到 react 模块的 react-native 应用程序创建了一个新的:

Unable to resolve module react from [myDir]\node_modules\@react-native-community\netinfo\src\index.ts: react Could not be found within the project.

If you are sure the module exists,try these steps:
 1. Clear watchman watches: watchman watch-del-all
 2. Delete node_modules and run yarn install
 3. Reset Metro's cache: yarn start --reset-cache
 4. Remove the cache: rm -rf /tmp/metro-*

我目前无权共享存储库,但当然,如果需要更多信息,我会立即提供

解决方法

现在,我已经通过回溯我的步骤解决了这个问题。也就是说,我从 nohoist 中删除了 react 并通过在根 package.json 中添加以下代码来解决重复的 react 模块问题:

  "resolutions": {
    "react": "^17.0.2","react-dom": "^17.0.2"
  }

然而,我不相信它是最好的解决方案,也不相信将来不会给我带来问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。