In Node.js and WebStorm, we want to avoid using require()
with relative file paths, e.g.
require('../../../../../ugly/path/to/file')
We've tried using the include function from here:
https://coderwall.com/p/th6ssq/absolute-paths-require
global.base_dir = __dirname;
global.abs_path = function(path) {
return base_dir + path;
}
global.include = function(file) {
return require(abs_path('/' + file));
}
global.include
is basically require(), but uses an absolute path, eliminating the need for all the ../../../.
The problem is that WebStorm method hinting and autocompletion doesn't work with this. So is there any workaround that allows for WebStorm method completion AND absolute require() paths?
Copyright Notice:Content Author:「Jordan Yim」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/39675697/node-js-require-code-completion-in-webstorm