You can use an npm package called module-alias
npm install module-alias --save
Then in your index.js file (assuming it is your startup file) configure your module aliases as follows:
const moduleAlias = require('module-alias');
moduleAlias.addAliases({
'@': __dirname, //Your project root
'@lib': __dirname + '/lib', //Path to your lib folder
'@routes': __dirname + '/routes' //Path to your routes folder
});
Then in the rest of your application, just load modules as follows:
const routes = require('@routes');
const coolStuff = require('@lib/coolStuff');
const app = require('@/app');
Relative routes sucks for one big reason, that if you move a file, you will need to change your require path otherwise your code will break.
One big setback with this approach is that the code autocompletion will fail to work, as your IDE will not be able to know how to locate the imported package.
If you can live without code autocompletion, it just can't get better than that.