Rewrite Imports Utility
rewrite-imports is a lightweight (349B) utility designed to transform ECMAScript `import` statements into CommonJS `require()` calls using regular expressions. As of its current stable version, 3.0.0, it provides a performant way to transpile modules at the string level, without relying on an Abstract Syntax Tree (AST) parser. This makes it extremely fast and suitable for build-time processing in environments where a full AST transformation is overkill or undesired. The package typically sees minor updates and patches, with major versions introducing breaking changes like the shift to named exports in v3.0.0. Its primary differentiator is its simplicity and reliance on RegExp, offering a niche solution for projects that need a quick, no-frills import rewriting mechanism, particularly for older Node.js environments (>=6) or browser environments with a `require` shim. Unlike full module bundlers or transpilers, it solely performs string replacement and does not provide a runtime or evaluate the transformed code.
Common errors
-
TypeError: rewrite-imports is not a function
cause Attempting to use `rewrite-imports` v3.0.0+ as a default export, typically `const rewrite = require('rewrite-imports')` or `import rewrite from 'rewrite-imports'`.fixUpdate your import statement to `import { rewrite } from 'rewrite-imports'` for ESM or `const { rewrite } = require('rewrite-imports')` for CommonJS. -
Transformed code contains syntax errors or unexpected runtime behavior related to module loading.
cause `rewrite-imports` uses regular expressions and may incorrectly transform strings that resemble import statements but are not actual module imports (e.g., in comments, multiline strings, or variable names).fixReview the original code for potential false positives where `import` keywords are used outside of actual module declarations. If this issue persists, consider using an AST-based transpiler for more robust code analysis.
Warnings
- breaking The primary `rewrite` function was migrated from a default export to a named export. This requires updating all import/require statements.
- breaking The utility no longer forces semicolon termination after rewritten `require()` statements. The output will now respect the original import statement's termination or lack thereof.
- breaking The `interop` function, which handled `default` exports from ESM/CommonJS modules, was removed in favor of a custom `require` function parameter. Users relying on `interop` will need to adjust their code.
- gotcha This module uses regular expressions for transformation, which can lead to 'false positives' if `import` keywords appear in comments, strings, or other non-module contexts. It is not an AST parser.
- gotcha The output requires a JavaScript runtime that supports CommonJS `require` calls and object destructuring assignments. This means Node.js versions `>=6.x` or browsers with a `require` shim and ES6 destructuring support.
- gotcha The utility only performs string transformation and does not provide a module runtime or evaluate the output. It is purely a build-time string manipulation tool.
Install
-
npm install rewrite-imports -
yarn add rewrite-imports -
pnpm add rewrite-imports
Imports
- rewrite
import rewrite from 'rewrite-imports'
import { rewrite } from 'rewrite-imports' - rewrite
const rewrite = require('rewrite-imports')const { rewrite } = require('rewrite-imports') - rewrite
rewrite(codeString)
Quickstart
import { rewrite } from 'rewrite-imports';
// Example 1: Default import
console.log(rewrite(`import foo from '../bar'`));
// Expected output: const foo = require('../bar');
// Example 2: Named import
console.log(rewrite(`import { foo } from 'bar'`));
// Expected output: const { foo } = require('bar');
// Example 3: Namespace import
console.log(rewrite(`import * as path from 'path';`));
// Expected output: const path = require('path');
// Example 4: Renamed named imports
console.log(rewrite(`import { foo as bar, baz as bat, lol } from 'quz';`));
// Expected output: const { foo:bar, baz:bat, lol } = require('quz');
// Example 5: Mixed default and named imports
console.log(rewrite(`import foobar, { foo as FOO, bar } from 'foobar';`));
// Expected output:
// const foobar = require('foobar');
// const { foo:FOO, bar } = foobar;