{"id":19112,"library":"babel-plugin-module-rewrite","title":"Babel plugin for dynamic module path rewriting","description":"Babel plugin that rewrites module import paths (both `import` and `require`) at compile time using a custom replacement function. Version 0.2.0 (latest) appears to be a stable, early-stage package with no active releases in recent years. It allows dynamic path transformations based on the calling file, unlike static alias plugins like babel-plugin-module-resolver. No major updates or security fixes have been published; the package should be considered minimal-risk but potentially unmaintained. Key differentiator is the per-file dynamic replacement capability.","status":"maintenance","version":"0.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/liady/babel-plugin-module-rewrite","tags":["javascript","babel","babel-plugin","module","rewrite","resolve","dynamic","rename","require"],"install":[{"cmd":"npm install babel-plugin-module-rewrite","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-module-rewrite","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-module-rewrite","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as a peer to use the plugin","package":"babel","optional":false},{"reason":"May be needed depending on Babel version","package":"babel-core","optional":true}],"imports":[{"note":"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.","wrong":"module.exports = function(originalPath, callingFileName, options) { ... }","symbol":"default (replace function)","correct":"export default function replaceImport(originalPath, callingFileName, options) { ... }"},{"note":"Plugin is configured via .babelrc or programmatic API; no direct import needed in user code.","wrong":"const babel = require('babel-core')","symbol":"babel-plugin-module-rewrite (plugin usage)","correct":"import babel from '@babel/core';\n// or add to .babelrc: [\"module-rewrite\", { replaceFunc: \"./path\" }]"},{"note":"This package is a Babel plugin, not a library to import. You only configure it in .babelrc.","wrong":"import plugin from 'babel-plugin-module-rewrite'","symbol":"Babel visitor (if extending)","correct":"no default export from the plugin; it's used as a Babel plugin"}],"quickstart":{"code":"npm install --save-dev babel-plugin-module-rewrite\n\n// .babelrc\n{\n  \"plugins\": [\n    [\"module-rewrite\", {\n      \"replaceFunc\": \"./replace-module-paths.js\"\n    }]\n  ]\n}\n\n// replace-module-paths.js\nexport default function replaceImport(originalPath, callingFileName, options) {\n  if (callingFileName.indexOf('/utils/') !== -1) {\n    return originalPath.replace('~', 'utils');\n  } else {\n    return originalPath.replace('~', 'common');\n  }\n}\n\n// Source file: src/utils/test.js\nimport something from '~/moduleFile'; // becomes 'utils/moduleFile'","lang":"javascript","description":"Configures the plugin to rewrite '~' prefix to 'utils' or 'common' based on calling file path."},"warnings":[{"fix":"Use babel-plugin-module-resolver or upgrade with @babel/core adapter.","message":"Plugin requires Babel 6.x; not compatible with Babel 7+ without modification.","severity":"breaking","affected_versions":">=0.0.0"},{"fix":"Ensure the file uses `export default function` or `module.exports = function`.","message":"The replace function file must be a module that exports a default function; other export patterns may silently fail.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use babel-plugin-dynamic-import-node or a newer plugin for dynamic imports.","message":"Plugin does not transform dynamic imports (import()) due to Babel 6 limitations.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Check that your replace function always returns a string, even if no replacement needed (return originalPath).","cause":"The replace function does not return a string, or the originalPath is undefined.","error":"Error: Plugin babel-plugin-module-rewrite threw: TypeError: Cannot read property 'replace' of undefined"},{"fix":"Use an absolute path or a path relative to process.cwd().","cause":"The replaceFunc path is relative to the working directory, not the file's location.","error":"ReferenceError: [BABEL] unknown: Cannot find module './replace-module-paths.js'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}