Avery235
Avery235

Reputation: 5306

Webpack relative vs absolute path

src
|
+-- action.js
|
+-- dir1
       |
       +--dir2
            |
            +--dir3
                 |
                 +--file.js

To import action.js from file.js, I need to do require('../../../action').

Is it a good idea to configure webpack to use src as the root, and import with require('action')?

This avoids the need to check how many ../ are needed to import a module in a file in a deeply nested folder.

But we won't be able to tell if action is from node_modules or src.

Is there a better alternative that solves both of the above?

Upvotes: 1

Views: 662

Answers (1)

Jason Allshorn
Jason Allshorn

Reputation: 1625

The webpack docs example has the entry point about 4 levels deep. I would suggest focusing on making your folder structure less deep if ../../../ is cumbersome. About node modules. You don't need ../, because require('some-module') will automatically look up the correct node module.

Upvotes: 1

Related Questions