-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Importing files outside main directory #232
Comments
this is a behavior of the react-native packager: facebook/react-native#12241 |
@brentvatne thanks for the reply. Will this solution work with Expo? |
why not try it and find out? :P yes |
@brentvatne Wondering if I'm doing something wrong or whether this doesn't work anymore. |
Can confirm that this solution does not work. There is currently no way of importing a file from outside the app directory. Using symlinks doesn't work either. |
One suggestion and workaround I will add is that you could bundle the common files into a npm package and publish it on the npm registry, then you can add it as a dependency to your projects. |
Metro Bundler, which is used by React Native as the packager (the React Native equivalent to Webpack) doesn't support symlinks. As for
An example of a
There really needs to be some better documentation on this :( If someone gets it working, can you write it up? |
@brentvatne I can't seem to get the packager to accept the other root directories. How do you import them? Do you need to reference the path or just call the component itself? 1 - I'm trying to get a monorepo working similar to yours but I just can't seem to crack this. My project directory
app.json
rn-cli.config.js
|
Nevermind! I'm an idiot! I misunderstood "under expo" in the app.json - It's all working now. app.json
|
@robdonn @brentvatne did this work for you when the module used There's nothing fancy going on with the library (https://github.com/viewsdx/keyboard-avoiding-and-dismissing-view), just one file that exports a component and imports react and some stuff from react-native. Both, Using the config above it picks up the module alright but it fails to load If It doesn't matter if my module comes before or after It is important to say that it works fine if my linked module doesn't use I wonder if anyone ran into this and got around it somehow? Do you reckon I should be posting this at facebook/metro/issues/1 instead? |
@dariocravero did you ever find a solution to this problem? This is exactly what I'm up against right now. |
This is crazy. Please post a solution to this problem if anyone finds it. |
@pankaj-arunsingh I can’t find the original issue where I finally found this but basically add only the external module path you want to import. In my case my module had a dependency on React and React-Native so those are added as extraNodeModules. Hope this helps. Inside var path = require("path");
const metroBundler = require('metro-bundler');
var config = {
extraNodeModules: {
"react-native": path.resolve(__dirname, "node_modules/react-native"),
"react": path.resolve(__dirname, "node_modules/react"),
},
getProjectRoots() {
return [
// Keep your project directory.
path.resolve(__dirname),
path.resolve(__dirname, "../native"), // path to the external module
];
}
}
module.exports = config; |
@pankaj-arunsingh - post to https://github.com/facebook/metro-bundler and be sure to fill out their issue template if you think this should be easier or whatever! @roblafeve's solution works |
In case anyone is stuck with this and until the different issues are fixed, I made a little guide on how to use yarn workspaces with Create React App and Create React Native App (Expo) to share common code across. Hope you find it handy! |
@dariocravero your link didn't work .. |
Thanks @jonasdumas, we unlinked that domain from medium and I forgot to update the link here! |
@dariocravero I got it working using your guide, however the nature of my project requires using a classic react-native project. After ejecting are their some steps to get things working? Let me know thanks! |
@dariocravero and anyone using his crna-make-symlinks-for-yarn-workspaces module, if you are running a classic react native project(or a ejected crna project) you can remove |
@roblafeve your solution is working!!! |
In 2022, a variant of this is working for us. Above posted solutions didn't seem to work anymore, and the linked article was not expo-specific (didn't include the defaultConfig, which leads to assets missing when compiling a binary on EAS). Solution below:
|
Here's another 2022 update. It seems the only solution that worked for me was the official workspace documentation. Here it is:
|
Is there a similar solution for using static assets outside of the main directory? |
Where I have a directory for mobile and a directory for web which I'd like to share some common code (utils, etc). Currently, if I import a file outside the root directory of my generated app (outside where
App.js
is located), I get an error with no file found.Is this by design? Will this be supported in the future and is this an Expo limitation or RN?
Thanks
The text was updated successfully, but these errors were encountered: