require-alias-node

require-alias-node Build Status

A tool which could set an alias for the module’s path which is required in the require of nodejs or import of ES6

It’s the alias of require-import-alias

Install

1
npm install require-alias-node

Sometimes, we may use a module with a long path to be required, and the path may contain many level, it’s not very intuitive !

1
2
const component = require('../../../src/component');
const moduleA = require('../../../src/component/render/moduleA');

Now we can use this tool require-alias-node to set alias for the moduleA, is looks like:

1
2
3
4
5
6
7
const requireAliasNode = require('require-alias-node');
requireAliasNode.setAlias({
'component': '../../../src/component',
'moduleA': '../../../src/component/render/moduleA'
});
const moduleA = require('moduleA');
const moduleB = require('component/main/moduleB')'

and you can also use it with import in ES2015(set an alias is a module, and use the alias in anthor module)

setAlias

1
2
3
4
5
import requireAliasNode from 'require-alias-node'
requireAliasNode.setAlias({
'component': '../../../src/component',
'moduleA': '../../../src/component/render/moduleA'
});

using it

1
2
3
import component from 'component'
import moduleA from 'moduleA'
import moduleB from 'component/render/moduleB'

It’s so graceful !