How to have path alias in nodejs?

Howard picture Howard · Oct 19, 2015 · Viewed 10.8k times · Source

Let's say i have following codes:

var mod1 = require('../../../../ok/mod1');
var mod2 = require('../../../info/mod2');

It's not pretty coding like above, i am wondering if there is a way to configure the root resolver just like webpack-resolve-root in nodejs?

So far as i know, the NODE_PATH can be used to replace the root of node_modules, but that's not what i want. I'd like to have the resolver to resolve multiple folders in order.

Answer

Prusprus picture Prusprus · May 18, 2017

There is an npm package called module-alias that may do what you are looking for.