Registry / devops / babel-plugin-require-root-rewrite

babel-plugin-require-root-rewrite

JSON →
library1.0.2jsnpmunverified

A Babel plugin that transforms module path aliases (e.g., '~/' or '@') into relative paths, allowing developers to require files relative to a configured project root. Version 1.0.2, stable but low activity. It offers a pattern-based replacement with optional basePath and overrides for multi-root projects. Unlike webpack resolve aliases or TypeScript path mapping, this works purely at the Babel transpilation step, making it suitable for Node.js CJS/ESM builds.

npm install babel-plugin-require-root-rewrite
INSTALL
IMPORT
SIG · BABEL-PLUGIN-REQUI
B
babel-plugin-require-root-rewrite
devopsjavascriptv1.0.2
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.

plugin
// .babelrc { "plugins": ["babel-plugin-require-root-rewrite"] }
// Attempting to import in code const plugin = require('babel-plugin-require-root-rewrite');
This is a Babel plugin, not a runtime import. Configure it in .babelrc or babel.config.js.
default
import plugin from 'babel-plugin-require-root-rewrite'; // if using programmatic API
const plugin = require('babel-plugin-require-root-rewrite').default;
The plugin has a default export; when using CommonJS require, it returns the plugin function directly.
rewrite patterns
// In .babelrc with options ["babel-plugin-require-root-rewrite", { "pattern": "^~/", "basePath": "src" }]
// No regex pattern in pattern ["babel-plugin-require-root-rewrite", { "pattern": "~" }]
The pattern is treated as a regex; always anchor it (e.g., '^~/') to avoid accidental matches. Available since v1.0.0.

Configures the plugin via .babelrc to replace '~/' with relative paths from the 'src' basePath, with overrides for 'api' and 'server' directories.

// .babelrc { "plugins": [ ["babel-plugin-require-root-rewrite", { "pattern": "^~/", "basePath": "src", "overrides": ["api", "server"] }] ] } // src/containers/Register.js const BaseAuthForm = require('~/components/BaseAuthForm'); import BaseAuthForm from '~/components/BaseAuthForm';
Debug
Known issues
gotchaPattern is a regex string; ensure you escape special characters (e.g., '^~/') to avoid unintended replacements.
fix
Use a pattern like '^~/' or '^@/' with the caret anchor.
affects: >=1.0.0
gotchaBasePath must exist relative to the project root; otherwise, relative paths may be incorrect.
fix
Set basePath to an existing directory (commonly 'src' or 'lib').
affects: >=1.0.0
gotchaOverrides only apply if the file path starts with one of the override strings; all other files use basePath.
fix
List directories (not file paths) in overrides array.
affects: >=1.0.0
deprecatedThe package uses the legacy Babel 6/7 API; may not work with Babel 8 without updates.
fix
Check compatibility with your Babel version; consider alternatives like babel-plugin-module-resolver.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Plugin .babelrc: [babel-plugin-require-root-rewrite] pattern is not a string
Pattern option is not a string or is missing.
fix
Provide a string pattern, e.g., '^~/'
SyntaxError: Unexpected token / in JSON at position 0
Invalid .babelrc JSON, possibly due to missing quotes or trailing commas.
fix
Validate your .babelrc with a JSON linter.
Cannot find module 'babel-plugin-require-root-rewrite'
Plugin not installed or not in node_modules.
fix
Run 'npm install babel-plugin-require-root-rewrite --save-dev'
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
babel-plugin-require-root-rewrite — npm install babel-plugin-require-root-rewrite · libregistry