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:
Enough of this madness!
Just create an alias and do it the right way:
var module = // Or ES6
It also allows you to register directories that will act just like node_modules
but with your own private modules, so that you can access them directly:
;// Or ES6
WARNING: This module should not be used in other npm modules since it modifies the default require
behavior! It is designed to be used for development of final projects i.e. web-sites, applications etc.
Install
npm i --save module-alias
Usage
Add your custom configuration to your package.json
(in your application's root)
// Aliases"_moduleAliases": "@root" : "." // Application's root "@deep" : "src/some/very/deep/directory/or/file" "@my_module" : "lib/some-file.js" "something" : "src/foo" // Or without @. Actually, it could be any string // Custom module directories, just like `node_modules` but with your private modules (optional)"_moduleDirectories": "node_modules_custom"
Then add this line at the very main file of your app, before any code
And you're all set! Now you can do stuff like:
const module = const veryDeepModule = const customModule = // module from `node_modules_custom` directory // Or ES6
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:
const moduleAlias = //// Register alias//moduleAlias // Or multiple aliasesmoduleAlias // Custom handler function (starting from v2.1)moduleAlias //// Register custom modules directory//moduleAliasmoduleAlias //// Import settings from a specific package.json// // Or let mudule-alias to figure where your package.json is// located. By default it will look in the same directory// where you have your node_modules (application's root)
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!
// webpack.config.jsconst npm_package = moduleexports = entry: ... resolve: root: __dirname alias: npm_package_moduleAliases || {} modules: npm_package_moduleDirectories || // eg: ["node_modules", "node_modules_custom", "src"]
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.