score:2

you'll have to do this:

  1. add a package.json file with { “name”: “folder_name” } in it to the folder you’d like to import from.
  2. import thing from ‘folder_name/thing’ or import thing from ‘./thing’

…except when going “up” in the folder structure.

import blah from ‘../../../../../blah’

in your react native project, chances are you keep your code in a single folder, such as “app”. if you have a directory named “app” this is what an absolute path might look like:

import thing from ‘awesomeapp/app/some/thing’

what sucks about this is literally all import statements (or require calls, if you’re still into that) would start with “awesomeapp/app/”, which is a lot to ask, when the alternative is simple to add a series of “../” (the key strokes are just so close together, it’s too easy).

to alleviate this pain point, you can simply add a package.json file inside the folder from where you want to import. in this case since all our code is under the “app” folder, we’d put the file here:

awesomeapp/app/package.json

then, add a “name” property to the json file with the folder name as its value (you can call it whatever you’d like, but really, that’ll just confuse people, including you in 6 months). the shorter, the better.

{ “name”: “app” }

now, you can import using that name as a reference.

import thing from ‘app/some/thing’


Related Query

More Query from same tag