首页 > 解决方案 > 使用带有 React 本机 init 的 yarn 工作区时缺少依赖项

问题描述

我想设置一个monorepo。我将我的 React 本机项目npx react-native init myProject作为第一个项目进行初始化。(后面会有更多项目添加)

文件夹结构

然后我从 myProject 的父文件夹设置 yarn 工作区。

    {   "name": "Parent",   
        "private": true,   
        "workspaces": {
        "packages": [
          "*"
        ],
        "nohoist": [
          "**/react-native",
          "**/react-native-*",
          "**/@react-native-*",
          "**/@react-native-*/**",
          "**/@react-navigation",
          "**/@react-navigation/**",
          "**/hermes-engine",
          "**/rn-*"
        ]   }
     }

一切似乎都有效,直到我推送到 git 并克隆回来。我使用yarn install但在启动项目时出现此错误(运行 android 或运行 ios)

Error: Unable to resolve module `scheduler` from `node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js`: scheduler could not be found within the project or in these directories:
  ..\node_modules

我可以解决的唯一方法是 cd myProject 并运行npm install(它会添加一些包并且应用程序将工作)而 cd 和 usingyarn install不会做任何事情

我只想在项目中使用纱线,那么我该怎么做才能解决这个问题?

标签: react-nativeyarnpkgmonorepoyarn-workspacesreact-native-cli

解决方案


我想办法解决它。我忘了编辑 RN 文件夹中的 metro.config。

const path = require('path')

const linkedLibs = [path.resolve(__dirname, '..')]
console.info('CONFIG', linkedLibs) 
 
module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  },
  watchFolders: linkedLibs,
};

使用它,一切都会好起来的


推荐阅读