JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1934788
  • Score
    100M100P100Q182090F
  • License MIT

Simple module for registering aliases of directories and custom module paths

Package Exports

  • module-alias
  • module-alias/register

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (module-alias) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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 creating 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)

// Aliases
"_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
}

// Custom modules directory (optional)
"_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:

import 'something'
import module from '@root/some-module'
import veryDeepModule from '@deep/my-module'
import myModule from '@my_module' // module from `node_modules_custom` directory

Advanced usage

import moduleAlias from 'module-alias'

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

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

//
// 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"]
  }
}

How it works?

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.

In order to register an alias it modifies the internal Module._resolveFilename method so that when you fire require or import it first checks whether the given string starts with one of the registered aliases, if so, it then replaces the alias in the string with the target path of the alias