简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM