Import -> absolute or relative path?

Hello everybody

is it better to use an absolute or relative path when using import ?

For example :
Import '/importe/ui/pages/foo-page '
Or
Import ‘…/…/ui/pages/foo-page’

=D

I tend to work by context.

I use relative paths if I’m importing something that’s related to the current file (e.g. containers and their respective components, which tend to be in their own subdirectories, like in the mantra spec). I guess for anything in the same “module”.

I use root level paths when I want something a core utility function (e.g. /lib) or anything from another module. If I understood your directory structure correctly, I’d use (personally) /imports/ui/pages/foo-page if I’m coming from something non-ui related.

A possibly a rule of thumb might just be that whatever is shorter for that case is probably what you’re after :>

1 Like

Thanks for your reply =D

I understand the way you do, but imagine that I used the following recommended structure :

|imports
    |startup
        |client
            - index.js
    |ui
        |pages
            - foo-page.js
            - foo-page.html
|client
    - main.js
```

In /imports/startup/client/index.js file that I use as import file, if I follow your reasoning , I use:
```` js
import '/imports/ui/pages/foo-page';
```
instead of
```js
import '../../ui/pages/foo-page';
```

Thanks for your help anyway ;-)
1 Like

I always use imports like:

import '/imports/ui/pages/foo-page

I consider that using ../ if bad for readability and makes it more difficult if you want to copy-paste code the same import in other modules.

6 Likes

I think the absolute path is good programming practice as it brings the benefit of ‘Don’t make me think’ and follows Occam’s razor principle.

The relative path is clearly a ‘feature’ and it should bring some benefit when used.