Bear with me, I'm not sure if this is purely a React Native issue, or just an ES6 question in general. But I noticed I'm unable to do this:
import {navBarRouteMapper} from '/src/helpers';
I get an error saying it's unable to resolve the module. I have to do this instead:
import {navBarRouteMapper} from '../../../src/helpers';
Keeping track of folder depth can get a bit unmanageable as the plexity of the app grows. Why am I not able to use an absolute path?
EDIT:
I see people are remending adding babel, but I don't want to pollute React Native's system. There's obviously transpilation to ES6 already going on. I was hoping for a solution specific to the React Native ecosystem.
Bear with me, I'm not sure if this is purely a React Native issue, or just an ES6 question in general. But I noticed I'm unable to do this:
import {navBarRouteMapper} from '/src/helpers';
I get an error saying it's unable to resolve the module. I have to do this instead:
import {navBarRouteMapper} from '../../../src/helpers';
Keeping track of folder depth can get a bit unmanageable as the plexity of the app grows. Why am I not able to use an absolute path?
EDIT:
I see people are remending adding babel, but I don't want to pollute React Native's system. There's obviously transpilation to ES6 already going on. I was hoping for a solution specific to the React Native ecosystem.
Share Improve this question edited Oct 9, 2017 at 19:38 balajeerc 4,1087 gold badges37 silver badges53 bronze badges asked Jan 25, 2016 at 20:28 ffxsamffxsam 27.8k34 gold badges100 silver badges150 bronze badges 5- 1 Possible duplicate of Importing node modules from root directory using es6 and babel-node. React Native uses Babel so I believe your answer lies here. – Mike Cluck Commented Jan 25, 2016 at 20:30
- You can do this with Browserify. – elclanrs Commented Jan 25, 2016 at 20:30
- do note that the path is a string, and need not be hard-coded in each place it's used. you can use the run time importing too. – dandavis Commented Jan 25, 2016 at 20:32
- This doesn't really have anything to do with ES6, since module loading is not part of ES6. – Felix Kling Commented Jan 26, 2016 at 1:40
- You are missing the module name in your require, check this answer stackoverflow./a/35819147/580167 – wdev Commented Jun 30, 2016 at 14:44
1 Answer
Reset to default 4There is actually a pretty clean solution for React Native, have a look here: https://medium./@davidjwoody/how-to-use-absolute-paths-in-react-native-6b06ae3f65d1#.u47sl3p8x.
TL;DR:
You'll just have to create a package.json
file in your src/helpers
folder:
{
"name": "@helpers"
}
And you will be able to import it from anywhere:
import { navBarRouteMapper } from '@helpers'