Cannot use absolute paths for import?

Feel me, I'm not sure if this is just a reputation issue, or just an ES6 issue in general. But I noticed that I can not do this:

import {navBarRouteMapper} from '/src/helpers'; 

I get a message that he cannot solve this module. I should do this instead:

 import {navBarRouteMapper} from '../../../src/helpers'; 

Folder depth tracking can become a little unmanageable as application complexity grows. Why can't I use the absolute path?

EDIT:

I see that people recommend adding babel, but I don't want to pollute the React Native system. Obviously, the broadcast to ES6 is already ongoing. I was hoping for a solution specific to the React Native ecosystem.

+5
source share
1 answer

There is actually a pretty clean solution for React Native, look here: https://medium.com/@davidjwoody/how-to-use-absolute-paths-in-react-native-6b06ae3f65d1#.u47sl3p8x .

TL DR:

You just need to create the package.json file in the src/helpers :

 { "name": "@helpers" } 

And you can import it from anywhere:

 import { navBarRouteMapper } from '@helpers' 
+5
source

Source: https://habr.com/ru/post/1241445/


All Articles