Donations are much appreciated and would help me continue working on this package! Donate here ❤️.
Create aliases of directories and register custom module paths in NodeJS like a boss!
No more long paths in Node, like:
import something from ('../../../../some/very/deep/module');
Enough of this madness!
Just create an alias and do it the right way:
import module from '@deep/module'// Or CommonJSconst module = require('@deep/module')
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:
import module from 'my_private_module'// Or CommonJSconst module = require('my_private_module')
WARNING: If you are going to use this package within another NPM package, please read Using within another NPM package first to be aware of potential caveats.
Install
npm i --save module-alias
Usage
ES Modules (Node 18.19+)
Add your custom configuration to your package.json (in your application's root):
node --import module-alias/register ./app.mjs # Or use a custom registerer, see below
Why the --import flag?
Unlike CommonJS, you cannot import module-alias/register at runtime. All import statements are hoisted and resolved before any code runs. The --import flag loads the loader hooks before your application starts.
Programmatic ESM Usage (Node 22.15+):
For programmatic alias registration, create a custom loader file:
// my-aliases.mjsimport { addAlias, addAliases } from 'module-alias'addAlias('@utils', process.cwd() + '/src/utils')// oraddAliases({ '@utils': process.cwd() + '/src/utils', '@lib': process.cwd() + '/src/lib'})// Custom handler functionaddAlias('@src', (fromPath, request, alias) => { // fromPath - Full path of the file from which `import` was called // request - The path that was passed into `import` (e.g. '@src/utils.js') // alias - The alias being matched (`@src` in this case) const subpath = request.slice(alias.length) // e.g. '/utils.js'
Then use it with the --import flag:
node --import ./my-aliases.mjs ./app.mjs
CommonJS (older Node 12+ versions)
Add your custom configuration to your package.json:
addPath(path) - Register custom modules directory (like node_modules, but with your own modules)
Examples:
const moduleAlias = require('module-alias')//// Register alias//moduleAlias.addAlias('@client', __dirname + '/src/client')// Or multiple aliasesmoduleAlias.addAliases({ '@root' : __dirname, '@client': __dirname + '/src/client', ...})// Custom handler function (starting from v2.1)moduleAlias.addAlias('@src', (fromPath, request, alias) => { // fromPath - Full path of the file from which `require` was called // request - The path (first argument) that was passed into `require` // alias - The same alias that was passed as first argument to `addAlias` (`@src` in this case) // Return any custom target path for the `@src` alias depending on arguments
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!
Unfortunately, module-alias itself would not work from Jest due to a custom behavior of Jest's require. But you can use it's own aliasing mechanism instead. The configuration can be defined either in package.json or jest.config.js. The example below is for package.json:
You can use module-alias within another NPM package, however there are a few things to take into consideration.
As the aliases are global, you should make sure your aliases are unique, to avoid conflicts with end-user code, or with other libraries using module-alias. For example, you could prefix your aliases with '@my-lib/', and then use require('@my-lib/deep').
The internal "register" mechanism may not work, you should not rely on require('module-alias/register') for automatic detection of package.json location (where you defined your aliases), as it tries to find package.json in either the current working directory of your node process, or two levels down from node_modules/module-alias. It is extremely likely that this is end-user code. So, instead, your should either register aliases manually with moduleAlias.addAlias, or using something like require('module-alias')(__dirname).
Front-end JavaScript code. Module-alias is designed for server side so do not expect it to work with front-end frameworks (React, Vue, ...) as they tend to use Webpack. Use Webpack's resolve.alias mechanism instead.
Jest, which discards node's module system entirely to use it's own module system, bypassing module-alias.