Socket
Socket
Sign inDemoInstall

module-alias

Package Overview
Dependencies
0
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    module-alias

Simple module for registering aliases of directories and custom module paths


Version published
Weekly downloads
1.1M
decreased by-2.31%
Maintainers
1
Install size
7.94 kB
Created
Weekly downloads
 

Package description

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

Readme

Source

module-alias

NPM Version Build Status

Allows to register aliases of directories and custom module paths in NodeJS.

This package is highly inspired by app-module-path package and it's totally backwards compatible with it. The main difference is that this package also allows you to create aliases of directories for further usage with require/import

Install

npm i --save module-alias

Usage

Add these lines to your package.json (in your application's root)

"_moduleDirectories": ["node_modules_custom"],
"_moduleAliases": {
  "@root"      : "", // Application's root
  "@client"    : "src/client",
  "@admin"     : "src/client/admin",
  "@deep"      : "src/some/very/deep/directory",
  "@my_module" : "src/some-file.js",
  "something"  : "src/foo", // Or without @. Actually, it could be any string
}

And 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
import 'something'
import foo from '@foo'
import deepModule from '@bar/my-module'
import module from 'some-module' // module from `node_modules_custom` directory

Advanced usage

import moduleAlias from 'module-alias'

//
// Register alias
//
moduleAlias.addAlias('@server', __dirname + '/src/server')

// Or multiple aliases
moduleAlias.addAliases({
  '@root'  : __dirname,
  '@server': __dirname + '/src/server',
  ...
})

//
// Register custom modules directory (like node_modules, but
// with your own modules)
//
moduleAlias.addPath(__dirname + '/node_modules_custom')
moduleAlias.addPath(__dirname + '/src')

//
// Import settings from package.json
//
moduleAlias(__dirname + '/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)
moduleAlias()

Usage with WebPack

// webpack.config.js
const npm_package = require('./package.json')

module.exports = {
  entry: { ... },
  resolve: {
    root: __dirname,
    alias: npm_package._moduleAliases || {},
    extensions: ['', '.js', '.jsx'],
    modulesDirectories: npm_package._moduleDirectories || [] // eg: ["node_modules", "node_modules_custom", "src"]
  }
}

Tags

Require alias, node import alias, node custom module directory, node local require paths, register module directory in nodejs

Keywords

FAQs

Last updated on 20 Feb 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc