Registry / web-framework / babel-plugin-module-resolver

babel-plugin-module-resolver

JSON →
library5.0.3jsnpmunverified

A Babel plugin that allows custom module resolution by defining root directories and aliases, eliminating deep relative imports like '../../../../utils'. It simplifies import/require paths for cleaner code. Current stable version is 5.0.3 (July 2024). Release cadence is irregular, with major version bumps occasionally (v4 in 2020, v5 in 2023). Key differentiators: supports both import and require, works with Babel 7+, and integrates with ESLint via eslint-import-resolver-babel-module for linting. Alternative: TypeScript's paths in tsconfig.json but this plugin works for plain JavaScript/Babel projects.

npm install babel-plugin-module-resolver
INSTALL
IMPORT
SIG · BABEL-PLUGIN-MODUL
B
babel-plugin-module-resolver
web-frameworkjavascriptv5.0.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import plugin from 'babel-plugin-module-resolver'
const plugin = require('babel-plugin-module-resolver')
In .babelrc.js, use require.resolve('babel-plugin-module-resolver') for the plugin. Common mistake: using require directly without resolve.
resolvePath
import { resolvePath } from 'babel-plugin-module-resolver'
const { resolvePath } = require('babel-plugin-module-resolver').default
resolvePath is a named export for plugin authors. Available since v3. ESM-only in v5+.
types
import type { PluginOptions } from 'babel-plugin-module-resolver'
Type imports for TypeScript users. PluginOptions type can be used in .babelrc.ts.

Configures the plugin in .babelrc with root and alias, then shows a simplified import path.

// .babelrc { "plugins": [ ["module-resolver", { "root": ["./src"], "alias": { "utils": "./src/utils", "@components": "./src/components" } }] ] } // Before import MyComponent from '../../../../components/MyComponent'; // After import MyComponent from '@components/MyComponent';
Debug
Known issues
breakingV4 changed the way aliases are resolved: paths without leading ./ or ../ are now treated as module names, not relative paths. This may break aliases to npm packages.
fix
Prefix alias paths with ./ or ../ to enforce relative resolution, or update alias values to match the new semantics.
affects: 4.0.0
gotchaPlugins can't resolve paths during ESLint linting unless you also configure eslint-import-resolver-babel-module.
fix
Add eslint-import-resolver-babel-module to your ESLint config and set the same root/alias options.
affects: >=3.0.0
gotchaThe plugin does not affect runtime resolution; it only rewrites during Babel transpilation. Ensure your runtime environment has the same module resolution or bundler config.
fix
Configure Webpack resolve.alias or TypeScript paths separately for runtime.
affects: >=1.0.0
deprecatedThe 'cwd' option (deprecated in v5) will be removed. Use 'root' with absolute paths instead.
fix
Replace 'cwd' with 'root' and use absolute paths or './src' relative to project root.
affects: 5.0.0
Errors
Common errors & fixes
Module not found: Can't resolve 'utils/MyUtilFn' in '/path/to/file'
The alias 'utils' points to a directory that doesn't exist or is not a valid path.
fix
Ensure the alias value is correct and the directory exists. Use absolute paths or './src' relative to project root.
Error: Cannot find module 'babel-plugin-module-resolver'
Plugin is not installed as a devDependency.
fix
Run `npm install --save-dev babel-plugin-module-resolver` or `yarn add --dev babel-plugin-module-resolver`.
Error: The 'cwd' option is deprecated and will be removed in future versions.
Using the 'cwd' option in plugin configuration.
fix
Replace 'cwd' with 'root' and use absolute paths or './src' relative to project root.
Upgrade
Version history
5.0.3latest on npm
Audit
Dependencies
babel-plugin-module-resolveroptionalThe package itself; optional if using eslint-import-resolver-babel-module separately
Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
2
Resources
babel-plugin-module-resolver — npm install babel-plugin-module-resolver · libregistry