Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

babel-plugin-module-resolver

tleunen7.6mMIT5.0.2

Module resolver plugin for Babel

babel, babel-plugin, module, resolver, alias, rewrite, resolve, rename, mapping, require, import

readme

babel-plugin-module-resolver

Maintenance Status NPM version Build Status Linux Build Status Windows Coverage Status

A Babel plugin to add a new resolver for your modules when compiling your code using Babel. This plugin allows you to add new "root" directories that contain your modules. It also allows you to setup a custom alias for directories, specific files, or even other npm modules.

Description

This plugin can simplify the require/import paths in your project. For example, instead of using complex relative paths like ../../../../utils/my-utils, you can write utils/my-utils. It will allow you to work faster since you won't need to calculate how many levels of directory you have to go up before accessing the file.

// Use this:
import MyUtilFn from 'utils/MyUtilFn';
// Instead of that:
import MyUtilFn from '../../../../utils/MyUtilFn';

// And it also work with require calls
// Use this:
const MyUtilFn = require('utils/MyUtilFn');
// Instead of that:
const MyUtilFn = require('../../../../utils/MyUtilFn');

Getting started

Install the plugin

npm install --save-dev babel-plugin-module-resolver

or

yarn add --dev babel-plugin-module-resolver

Specify the plugin in your .babelrc with the custom root or alias. Here's an example:

{
  "plugins": [
    ["module-resolver", {
      "root": ["./src"],
      "alias": {
        "test": "./test",
        "underscore": "lodash"
      }
    }]
  ]
}

.babelrc.js version Specify the plugin in your .babelrc.js file with the custom root or alias. Here's an example:


const plugins = [
  [
    require.resolve('babel-plugin-module-resolver'),
    {
      root: ["./src/"],
      alias: {
        "test": "./test"
      }
    }

  ]

];

Good example: // https://gist.github.com/nodkz/41e189ff22325a27fe6a5ca81df2cb91

Documentation

babel-plugin-module-resolver can be configured and controlled easily, check the documentation for more details

Are you a plugin author (e.g. IDE integration)? We have documented the exposed functions for use in your plugins!

ESLint plugin

If you're using ESLint, you should use eslint-plugin-import, and eslint-import-resolver-babel-module to remove falsy unresolved modules. If you want to have warnings when aliased modules are being imported by their relative paths, you can use eslint-plugin-module-resolver.

Editors autocompletion

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "*": ["src/*"],
      "test/*": ["test/*"],
      "underscore": ["lodash"]
    }
  }
}
  • IntelliJ/WebStorm: You can mark your module directories as "resources root" e.g if you have ../../../utils/MyUtilFn you can mark ../../../utils as "resources root". This has the problem that your alias also has to be named utils. The second option is to add a webpack.config.js to your project and use it under File->Settings->Languages&Frameworks->JavaScript->Webpack. This will trick webstorm into resolving the paths and you can use any alias you want e.g.:
var path = require('path');

module.exports = {
  resolve: {
    extensions: ['.js', '.json', '.vue'],
    alias: {
      utils: path.resolve(__dirname, '../../../utils/MyUtilFn'),
    },
  },
};

License

MIT, see LICENSE.md for details.

Who is using babel-plugin-module-resolver ?

Are you also using it? Send a PR!

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

5.0.2 (2024-04-18)

Bug Fixes

  • Revert reselect update to fix usage in node 16 (#446) (8923c48)

5.0.1 (2024-04-17)

5.0.0 (2023-01-09)

⚠ BREAKING CHANGES

  • Remove support for Node < 16

  • Update dependencies and find-babel-config to fix json5 vulnerabilities (#441) (c43e71c)

4.1.0 (2020-12-15)

Features

4.0.0 (2019-12-11)

⚠ BREAKING CHANGES

  • The minimum node.js version that is now officially supported is Node 8.

Features

  • Add support for alias with array of paths (#376) (f2173ee)

3.2.0 (2019-02-11)

Features

  • dev: add loglevel to configure console logging (#351) (d4a596c)

3.1.3 (2019-01-21)

3.1.2 (2019-01-16)

Bug Fixes

3.1.1 (2018-03-24)

Bug Fixes

3.1.0 (2018-02-08)

Bug Fixes

  • Fix path transformation for dot files that are siblings (#253) (7dc2da6)

Features

  • Ability to declare aliases in an array to preserve the order (#243) (d03715d)
  • Add stripExtensions option to determine which extensions get removed (#247) (fdf5da9)
  • Allow using a function to define the result of an alias (#245) (9299d9a)
  • run plugin also on Program exit to handle dynamically added imports from other transforms (#269) (12a2d07)

3.0.0 (2017-11-08)

Features

  • Add jest.setMock to the list of transformed jest functions (#225) (a7c04b0)
  • Add require.requireActual and require.requireMock to the list of transformed Jest functions (#227) (47fd5ba)
  • Make alias config take precedence over root config (#221) (3c711f8)

BREAKING CHANGES

  • This makes alias taking precedence over the root config because it's usually more specific.

3.0.0-beta.5 (2017-08-17)

Bug Fixes

3.0.0-beta.4 (2017-07-10)

Bug Fixes

  • Prevent double application of the transform (#176) (feefe9e)

Features

3.0.0-beta.3 (2017-05-16)

3.0.0-beta.2 (2017-05-16)

Bug Fixes

  • package: update pkg-up to version 2.0.0 (#168) (ccba432)
  • Fix path resolution when the alias is not a proper sub path (#177) (dcd5dca)

Features

  • Add the .mjs extension to the default extensions (#172) (ea9e457)
  • Run the plugin in the "enter" phase of the Program (#179) (c4794f9)

3.0.0-beta.0 (2017-04-23)

Feat

  • warn when the package from resolved alias is not available (#160) (c937f79)

Features

  • Add support for new ES dynamic import() (#143) (ad65935)
  • Convert root to array if root is a string (#153) (2bcea0c), closes #152
  • Support 'packagejson' as a custom cwd option (#149) (52600af)

BREAKING CHANGES

  • The "npm:" prefix has been removed.

2.7.0 (2017-03-30)

Reverts

  • Force a 2.7.0 by reverting all changes since 2.5.0 (577afad)

2.6.2 (2017-03-30)

Bug Fixes

2.6.1 (2017-03-29)

Bug Fixes

  • Fix regExps.find undefined error (2171200)

2.6.0 (2017-03-29)

Bug Fixes

Features

2.5.0 (2017-02-05)

Bug Fixes

  • Add only directories in a glob root config (#120) (ac87924)
  • Remove unnecessary trailing /index in the local path resolution (#117) (ac0c671)

Features

  • Add support for jest doMock and dontMock functions (#114) (0bc01ec)
  • Export manipulateOptions (85270e1)
  • Export the default extensions used by the plugin (1c4fbcc)

2.4.0 (2016-11-30)

Features

2.3.0 (2016-11-06)

Bug Fixes

  • Fix wrong location of modified path (e7083ab), closes #94

Features

  • Move the babelrc lookup behind a custom cwd value option (2a8aca5)

2.2.0 (2016-08-27)

Bug Fixes

  • Fix plugin on windows (d6b0a4b)
  • Fix the root resolver when both a file and directory have the same name (a4cff68)

Features

  • Add glob support in the root config (#78) (1f6245b)

2.1.1 (2016-08-22)

Bug Fixes

  • Fix resolver with filename containing a dot (#75) (bb6c903), closes #74

2.1.0 (2016-08-19)

Bug Fixes

  • Fix root mapping with custom extensions (#72) (3d4756b)

Features

  • Aliasing a npm module without 'npm:' (#73) (8e95988)

2.0.0 (2016-08-14)

Features

  • Add support for custom root directories (#69) (3dd29a0), closes #46

BREAKING CHANGES

  • There's a new way to specify alias in the plugin options. See README for more info