Registry /
web-framework / babel-plugin-module-rewrite
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
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
default (replace function)
✓ export default function replaceImport(originalPath, callingFileName, options) { ... }
✗ module.exports = function(originalPath, callingFileName, options) { ... }
The replace function must be a default export from the file specified in the plugin options. Using CJS `module.exports` works, but plugin expects a default export in some setups.
babel-plugin-module-rewrite (plugin usage)
✓ import babel from '@babel/core';
// or add to .babelrc: ["module-rewrite", { replaceFunc: "./path" }]
✗ const babel = require('babel-core')
Plugin is configured via .babelrc or programmatic API; no direct import needed in user code.
Babel visitor (if extending)
✓ no default export from the plugin; it's used as a Babel plugin
✗ import plugin from 'babel-plugin-module-rewrite'
This package is a Babel plugin, not a library to import. You only configure it in .babelrc.
Configures the plugin to rewrite '~' prefix to 'utils' or 'common' based on calling file path.
npm install --save-dev babel-plugin-module-rewrite
// .babelrc
{
"plugins": [
["module-rewrite", {
"replaceFunc": "./replace-module-paths.js"
}]
]
}
// replace-module-paths.js
export default function replaceImport(originalPath, callingFileName, options) {
if (callingFileName.indexOf('/utils/') !== -1) {
return originalPath.replace('~', 'utils');
} else {
return originalPath.replace('~', 'common');
}
}
// Source file: src/utils/test.js
import something from '~/moduleFile'; // becomes 'utils/moduleFile'
Errors
Common errors & fixes
Error: Plugin babel-plugin-module-rewrite threw: TypeError: Cannot read property 'replace' of undefined
The replace function does not return a string, or the originalPath is undefined.
fixCheck that your replace function always returns a string, even if no replacement needed (return originalPath).
ReferenceError: [BABEL] unknown: Cannot find module './replace-module-paths.js'
The replaceFunc path is relative to the working directory, not the file's location.
fixUse an absolute path or a path relative to process.cwd().
Audit
Dependencies
babelrequiredRequired as a peer to use the plugin
babel-coreoptionalMay be needed depending on Babel version