-
Notifications
You must be signed in to change notification settings - Fork 24.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
Can't import js file in parent folder #12241
Comments
I don't believe that this is a bug, you should not be able to read files from outside of your project's root. |
@hnryjms and I ran in to this as well. It is an issue for us because our react-native app is a subdirectory of our repo that contains a web app and server code. We have a The solution we found was to add the file called const Path = require('path');
module.exports = {
getProjectRoots: () => [ __dirname, Path.join(__dirname, "..") ]
}; |
@gensc004 Can you specify? I'm going up two directories, but adding
Code inside
|
@doque, Just to make sure, have you rebuilt your project after adding the rn-cli config file? |
@gensc004 The error says that the directory doesn't exist. It's not a directory, it's a module, and the path to the module printed in the error is correct, but for some reason the packager can't find it. |
I've built the project numerous times. I've set up from-scratch using both |
Can someone reopen this ticket? |
@doque, @kylebebak What versions of react-native are you on? I checked back through my solution to see if I missed any steps and haven't found anything that seems amiss, but I will keep trying to help where I can. Also, I currently only use this solution for IOS and haven't tried it on android so that may be part of the problem. |
I just made a new project with react-native init, and tried the solution on IOS (I only have access to an IOS emulator at the moment). After adding the rn-config file, closing my bundler, and re-running the build it worked for me. The only thing I can see being a problem is that the solution doesn't work on Android. If someone could confirm that they are seeing the solution not working on Android that would be awesome, and then we can try to find a solution that works for both! |
I'm running these versions:
|
@doque, doesn't look like it is a version problem, are you testing on android or ios? |
I'm running |
Exact stuff happens with command 'react-native run-android'. What's even worse, I've also moved my source file into project's root and still receiving error 'Unable to resolve module ... Directory doesn't exist". |
I'm using Expo 18, which means I'm on RN 0.45.0. We found a work-around and no longer need to import modules from outside the project root, but it would be nice to know if you guys find a solution to this problem. |
@kylebebak what's your workaround? |
@doque I'm going to blog about it at some point. |
@kylebebak what's your workaround? :-) |
@brentvatne how should we be references shared libraries that might be used in either multiple react-native apps / a react web app where both the libraries and apps are being developed at the same time? I've been searching for the for 30 minutes now and can't find a working solution on StackOverflow / in other bugs etc. If it is possible, it seems like this deserves docs? @gensc004 do you have a simple working example of this or copy of code you can share of how you import the packages + the directory structure? I can't seem to get your solution to work either |
Has anyone had any luck with this? Neither |
Nope, you just have to have everything within the root of the RN project |
for anyone still having issues, combining the unfortunately this is more than a bug, its a design flaw in |
Finally, we found a solution - Haul. And here is an example project |
@ericketts can you upload please some sample project which shows how to solve the issue? |
@MPiwowarski check here for an example project, hope this helps! (and that it's actually what you're trying to do 😁) Edit: I should point out for haul, which has been suggested, realize that the lack of support for delta bundles could cause headaches down the road on Android, for instance react-native-navigation proved incompatible with haul because of the lack of delta bundle support. |
@ericketts thank you very much! It works properly. I've also added some sample react native component to your shared folder and imported it in App.js in rn folder and works fine :) Do you think your solution works also with Expo? Cheers |
@MPiwowarski I'm not sure whether expo would work with that configuration, to be honest I gave up on expo after their |
@ericketts I've made a fork from your solution, you can check it here I've added expo project to the src folder and also created Sample react native component in the shared folder. Both projects expo and rn import that component and use it in App.js. |
@ericketts and @MPiwowarski, thank you so much for putting together those repos. They were instrumental in helping us set up code sharing between our native and web apps. Many, many thanks, and I hope others will find this. This is probably the most simple solution I've found to sharing code. |
worth noting that this hack my break soon |
Also, were you able to get this to work with eslint? The |
I'm not sure I had that issue since I don't have any duplication of deps. We do use eslint and it all seems to work without any special configuration. |
Ah. Yeah, so switching over to using |
Nope, I spoke to soon. It looks like that config simply doesn't check the validity of imported modules. 😞 |
Description
Can't import js file in parent folder
Reproduction
import {ReactNativeCron} from '../ReactNativeCron' gives error:
Directory /Users/nikos/WebstormProjects/react-native-cron/ReactNativeCron doesn't exist
If I move the file to ./ReactNativeCron it works
React Native version: "react-native": "0.40.0"
Platform: iOS
Operating System:MacOS
see https://github.com/QuantumInformation/react-native-cron/commit/ea7fdf2b2d8e280d630baf855da4b4d888772195
The text was updated successfully, but these errors were encountered: