Registry / devops / babel-plugin-rewrite-require

babel-plugin-rewrite-require

JSON →
library1.14.5jsnpmunverified

A Babel plugin that rewrites ES6 module imports and CommonJS require() calls using a simple alias map. Version 1.14.5 is stable with infrequent releases. Key differentiators: supports optional module blacklisting, non-string literal detection, and missing file handling. Unlike generic alias plugins, it is designed for React Native and browserify compatibility, enabling runtime exceptions for optional dependencies or missing files.

npm install babel-plugin-rewrite-require
INSTALL
IMPORT
SIG · BABEL-PLUGIN-REWRI
B
babel-plugin-rewrite-require
devopsjavascriptv1.14.5
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
module.exports = require('babel-plugin-rewrite-require')
CommonJS-style export; used in Babel config files.
default
import rewrite from 'babel-plugin-rewrite-require'
import { rewrite } from 'babel-plugin-rewrite-require'
Default export only; named import will result in undefined.

Configures the plugin with aliases, non-string literal enforcement, and module/file blacklisting.

// .babelrc or babel.config.js { "plugins": [ ["babel-plugin-rewrite-require", { "aliases": { "some-module": "./some-replacement-module", "crypto": "crypto-browserify" }, "throwForNonStringLiteral": true, "throwForModules": ["optional-dep"], "throwForMissingFiles": ["/path/to/optional/config.json"] }] ] } // Input: require('some-module') becomes require('./some-replacement-module') // Input: require('crypto') becomes require('crypto-browserify') // Input: require('cry' + 'pto') throws a runtime error // Input: require('optional-dep') throws a runtime error // Input: require('/path/to/optional/config.json') throws a runtime error
Debug
Known issues
gotchaThe plugin is a CommonJS module; importing it in an ES module context may require dynamic import or Babel config as JS.
fix
Use module.exports in Babel config file, or use dynamic import with .mjs config.
affects: >=1.0
gotchaThe default export must be used; named exports do not exist.
fix
Use import pkg from 'babel-plugin-rewrite-require' or const pkg = require('babel-plugin-rewrite-require')
affects: >=1.0
deprecatedPlugin options structure may change in future major versions; aliases is currently flat.
fix
Use current flat object format for aliases.
affects: <2.0
Errors
Common errors & fixes
Error: Cannot find module 'babel-plugin-rewrite-require'
Plugin not installed or incorrectly referenced.
fix
npm install babel-plugin-rewrite-require --save-dev
TypeError: (0 , _plugin.default) is not a function
Using named import instead of default import.
fix
Use import rewrite from 'babel-plugin-rewrite-require'
ReferenceError: throwForModules is not a function
Options passed incorrectly (e.g., as a string instead of object in array form).
fix
Ensure plugin is specified as ["babel-plugin-rewrite-require", { ... }] in .babelrc
Upgrade
Version history
1.14.5latest on npm
Audit
Dependencies
@babel/coreoptionalRequires Babel core to function as a plugin.
Agent activity
10 hits · last 30 days
node
8
Amazon
1
Resources
babel-plugin-rewrite-require — npm install babel-plugin-rewrite-require · libregistry