Search code examples
react-nativeyarnpkgmonorepoyarn-workspacesreact-native-cli

Missing dependencies when using yarn workspace with React native init


I want to setup a monorepo. I init my React native project with npx react-native init myProject as the first project. (there will be more project added later)

Folder structure

  • Parent
    • myProject
      • package.json (created by react-native)
    • yarn.lock
    • package.json (where I setup workspace)

Then I setup yarn workspace from the parent folder of myProject.

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

Everything seems to work until I push to git and clone back. I use yarn install but got this error when start the project (run android or run 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

The only way I can fix that is to cd myProject and run npm install (it will add some packages and the app will work) while cd and using yarn install won't do anything

I just want to use yarn for the project so what can I do to fix this problem?


Solution

  • I figure out how to fix it. I forgot to edit metro.config in RN folder.

    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,
    };
    

    Use this and everything will be fine