Modules required from outside of root directory does not find node_modules
See original GitHub issueHi! 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:
- Created 6 years ago
- Reactions:50
- Comments:39 (4 by maintainers)
Top GitHub Comments
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+.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 aresolver
object in yourrn-cli.config.js
, and alsogetProjectRoots()
is no longer a thing. The example provided above would be rewritten as follows for RN 0.57+: