What is module-alias?
The module-alias package is used to create aliases of directory paths, allowing you to simplify the require/import statements in your Node.js projects. This can be particularly useful for projects with deep directory structures, making the code cleaner and easier to maintain.
What are module-alias's main functionalities?
Registering Aliases
This feature allows you to register aliases for directories so that you can require modules using the alias instead of relative paths.
require('module-alias/register');
moduleAlias.addAliases({
'@root' : __dirname,
'@models' : __dirname + '/models',
'@controllers': __dirname + '/controllers',
'@lib' : __dirname + '/lib'
});
Customizing Aliases with package.json
You can also define aliases directly in your package.json file, which module-alias will read and use to resolve modules.
{
"_moduleAliases": {
"@root": ".",
"@models": "./models",
"@controllers": "./controllers",
"@lib": "./lib"
}
}
Requiring Modules with Aliases
Once aliases are set up, you can require modules using the defined aliases, making the require statements much cleaner and easier to understand.
const User = require('@models/user');
Other packages similar to module-alias
require-alias
This package allows you to alias module paths in Node.js, similar to module-alias. It provides a way to keep your require calls clean. However, it is not as widely used or as well-maintained as module-alias.
babel-plugin-module-resolver
This Babel plugin allows you to add new 'root' directories that contain your modules. It also lets you map a module to another module or filepath. It is more flexible than module-alias as it integrates with Babel and supports both Node.js and frontend JavaScript projects.
link-module-alias
This package provides similar functionality to module-alias, allowing you to define aliases for your modules and directories. It differs in the way it sets up the aliases, using symbolic links, which can be more compatible with certain tools that do not understand module resolution.
module-alias
Create aliases of directories and register custom module paths in NodeJS like a boss!
No more shit-coding paths in Node like so:
require('../../../../some/very/deep/module')
Enough of this madness!
Just create an alias and do it the right way:
var module = require('@deep/module')
import module from '@deep/module'
Install
npm i --save module-alias
Usage
Add these lines to your package.json
(in your application's root)
"_moduleAliases": {
"@root" : ".",
"@client" : "src/client",
"@deep" : "src/some/very/deep/directory",
"@my_module" : "src/some-file.js",
"something" : "src/foo",
}
"_moduleDirectories": ["node_modules_custom"],
Then add these line at the very main file of your app, before any code
import 'module-alias/register'
And you're all set! Now you can do stuff like:
require('something')
const module = require('@root/some-module')
const veryDeepModule = require('@deep/my-module')
const customModule = require('my_custom_module')
import 'something'
import module from '@root/some-module'
import veryDeepModule from '@deep/my-module'
import customModule from 'my_custom_module'
Advanced usage
If you don't want to modify your package.json
or you just prefer to set it all up programmatically, then the following methods are available for you:
addAlias('alias', 'target_path')
- register a single aliasaddAliases({ 'alias': 'target_path', ... })
- register multiple aliasesaddPath(path)
- Register custom modules directory (like node_modules, but with your own modules)
Examples:
import moduleAlias from 'module-alias'
moduleAlias.addAlias('@client', __dirname + '/src/client')
moduleAlias.addAliases({
'@root' : __dirname,
'@client': __dirname + '/src/client',
...
})
moduleAlias.addPath(__dirname + '/node_modules_custom')
moduleAlias.addPath(__dirname + '/src')
moduleAlias(__dirname + '/package.json')
moduleAlias()
Usage with WebPack
Luckily, WebPack has a built in support for aliases and custom modules directories so it's easy to make it work on the client side as well!
const npm_package = require('./package.json')
module.exports = {
entry: { ... },
resolve: {
root: __dirname,
alias: npm_package._moduleAliases || {},
extensions: ['', '.js', '.jsx'],
modulesDirectories: npm_package._moduleDirectories || []
}
}
How it works?
In order to register an alias it modifies the internal Module._resolveFilename
method so that when you use require
or import
it first checks whether the given string starts with one of the registered aliases, if so, it replaces the alias in the string with the target path of the alias.
In order to register a custom modules path (addPath
) it modifies the internal Module._nodeModulePaths
method so that the given directory then acts like it's the node_modules
directory.