question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Modules required from outside of root directory does not find node_modules

See original GitHub issue

Hi! I hope I post this in the right place, sorry if I’m not.

I’m trying to setup requiring files outside of the root project directory, as I’m building both a web app and a native app that’ll share some code. I’ve configured this using rn-cli.config.js, and I can successfully import my shared files from outside of my root.

However, the files imported from outside of the root does not find any modules from node_modules (React for example). Files required from inside of the project root does find them (which I guess is because node_modules is in a parent directory to them). I just get a Cannot resolve module 'React'.

The folder structure looks like this:

common
- components
- - ....code
web
- node_modules
- ....code
native
- node_modules
- ...code, rn-cli.config.js, etc...

rn-cli.config.js:

const path = require('path');

module.exports = {
  getProjectRoots() {
    return [
      path.resolve(__dirname, '../common'),
      path.resolve(__dirname, 'node_modules'),
      path.resolve('.')
    ]
  }
};

If I add a package.json to common and install react there it works. So I guess this has to do with the packager walking the file upwards and not finding node_modules as it’s in a sibling directory and not a parent directory?

Am I missing something obvious here? I’ve tried clearing the cache etc. I’m on the latest Expo which I’m pretty sure uses RN 0.43.

Thanks in advance!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:50
  • Comments:39 (4 by maintainers)

github_iconTop GitHub Comments

59reactions
fiznoolcommented, Jul 3, 2019

I’m seeing the same thing too @Edmond-XavierCollot - thank you @denwakeup for the fix. Here’s an updated metro.config.js file that should be used for RN 0.59+.

const path = require('path');

module.exports = {
  /**
   * Ensure any imports inside the shared 'components' folder resolve to the local node_modules folder
   */
  resolver: {
    extraNodeModules: new Proxy({}, {
      get: (target, name) => path.join(process.cwd(), `node_modules/${name}`),
    }),
  },
 /**
   * Add our workspace roots so that react native can find the source code for the included packages
   * in the monorepo
   */
  projectRoot: path.resolve(__dirname),
  watchFolders: [
    path.resolve(__dirname, "../../components"),
    path.resolve(__dirname, "../../node_modules")
  ]
}
44reactions
fiznoolcommented, Oct 15, 2018

We actually ended up adding nohoist: ['**'] to our react-native yarn workspace because it was easier than figuring out which needed to be where.

I came to the same conclusion too! Yes I end up with a larger workspace but the benefits of not having to hunt these things down far outweigh the cons of a larger workspace size.

As mentioned above, for RN 0.57 you have to put your extraNodeModules inside a resolver object in your rn-cli.config.js, and also getProjectRoots() is no longer a thing. The example provided above would be rewritten as follows for RN 0.57+:

module.exports = {
  /**
   * Add "global" dependencies for our RN project here so that our local components can resolve their
   * dependencies correctly
   */
  resolver: {
    extraNodeModules: {
      react: path.resolve(__dirname, "node_modules/react"),
      "react-native": path.resolve(__dirname, "node_modules/react-native"),
      "@storybook": path.resolve(__dirname, "node_modules/@storybook")
    }
  },
 /**
   * Add our workspace roots so that react native can find the source code for the included packages
   * in the monorepo
   */
  projectRoot: path.resolve(__dirname),
  watchFolders: [
    path.resolve(__dirname, "../../components"),
    path.resolve(__dirname, "../../node_modules")
  ]
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I put the npm node_modules directory outside of my project
When you require a module node.js will search for a node_modules directory from the current directory upwards. That means if it can't find...
Read more >
node_modules folder is always marked as "library root" with ...
That's correct. package.json is only once, in root of Node application. Therefore, top-level node_modules directory is intended only for third-party ...
Read more >
Requiring modules in Node.js: Everything you need to know
Besides resolving modules from within the node_modules directories, we can also place the module anywhere we want and require it with either relative...
Read more >
CommonJS modules | Node.js v19.3.0 Documentation
When the entry point is not a CommonJS module, require.main is undefined ... Node.js will not append node_modules to a path already ending...
Read more >
Dependency resolution - Parcel
node_modules directories are searched upwards from the importing file. The search stops at the project root directory. For example, if the importing file ......
Read more >

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found