react-native cannot find node_modules that exists. this probably only occurs when I use yarn workspaces

Viewed 588

In order to add it I ran the following command:

yarn workspace mobile add react-native-webview

I literally have looked in the node_modules folders that is specific by the error below and react-native-webview is clearly in there. However, this error is still always thrown? I even did the steps react-native outlines on the red error screen says when a module is shown as missing but is actually there.

Error: Unable to resolve module `react-native-webview` from `index.js`: react-native-webview could not be found within the project or in these directories:
  ../../node_modules
  /Users/kurnalsaini/Documents/test-mono/packages/mobile/node_modules

Am I suppose to be modifying my metro-config file or some other config file to get this working? Because to me it's boggling that it's telling it's looking in the correct folders and still saying it doesn't exist.

1 Answers

Try to set your metro config to watch for node_modules in the directory shown in the error, your metro.config.js should look like this: const path = require("path");

    const watchFolders = [
      //Relative path to packages directory
      path.resolve(__dirname + "/.."),
      path.resolve(__dirname + "/../../node_modules"),
    ];
    
    module.exports = {
      resetCache: true,
      transformer: {
        getTransformOptions: async () => ({
          transform: {
            experimentalImportSupport: false,
            inlineRequires: false,
          },
        }),
      },
      watchFolders,
    };
Related