{"id":11950,"library":"rewrite-imports","title":"Rewrite Imports Utility","description":"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.","status":"active","version":"3.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/lukeed/rewrite-imports","tags":["javascript","regex","rewrite","modules","imports","requires","aliases","typescript"],"install":[{"cmd":"npm install rewrite-imports","lang":"bash","label":"npm"},{"cmd":"yarn add rewrite-imports","lang":"bash","label":"yarn"},{"cmd":"pnpm add rewrite-imports","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since v3.0.0, `rewrite` is a named export. Default import will result in a TypeError.","wrong":"import rewrite from 'rewrite-imports'","symbol":"rewrite","correct":"import { rewrite } from 'rewrite-imports'"},{"note":"Since v3.0.0, `rewrite` is a named export. Direct CommonJS require without destructuring will result in a TypeError.","wrong":"const rewrite = require('rewrite-imports')","symbol":"rewrite","correct":"const { rewrite } = require('rewrite-imports')"},{"note":"The primary API is a single function `rewrite`.","symbol":"rewrite","correct":"rewrite(codeString)"}],"quickstart":{"code":"import { rewrite } from 'rewrite-imports';\n\n// Example 1: Default import\nconsole.log(rewrite(`import foo from '../bar'`));\n// Expected output: const foo = require('../bar');\n\n// Example 2: Named import\nconsole.log(rewrite(`import { foo } from 'bar'`));\n// Expected output: const { foo } = require('bar');\n\n// Example 3: Namespace import\nconsole.log(rewrite(`import * as path from 'path';`));\n// Expected output: const path = require('path');\n\n// Example 4: Renamed named imports\nconsole.log(rewrite(`import { foo as bar, baz as bat, lol } from 'quz';`));\n// Expected output: const { foo:bar, baz:bat, lol } = require('quz');\n\n// Example 5: Mixed default and named imports\nconsole.log(rewrite(`import foobar, { foo as FOO, bar } from 'foobar';`));\n// Expected output:\n// const foobar = require('foobar');\n// const { foo:FOO, bar } = foobar;","lang":"javascript","description":"Demonstrates how to use the `rewrite` function to convert various ES import syntaxes into CommonJS require() statements."},"warnings":[{"fix":"Change `import rewrite from 'rewrite-imports'` to `import { rewrite } from 'rewrite-imports'` or `const rewrite = require('rewrite-imports')` to `const { rewrite } = require('rewrite-imports')`.","message":"The primary `rewrite` function was migrated from a default export to a named export. This requires updating all import/require statements.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"No direct fix needed unless your build process relied on the forced semicolon. Review your linting rules if this causes issues.","message":"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.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"If you relied on `interop`, you may need to manually manage default exports or adjust your custom `require` function (via the `fn` parameter) to handle interop behavior.","message":"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.","severity":"breaking","affected_versions":">=1.4.0"},{"fix":"For highly complex codebases or strict accuracy, consider using an AST-based transpiler instead. For simpler cases, ensure `import` statements are clean and unambiguous.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure your target runtime environment meets the Node.js `>=6.x` requirement or provides necessary polyfills/shims for browser compatibility.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Do not expect `rewrite-imports` to handle module loading or execution at runtime; it's designed for static code transformation.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Update your import statement to `import { rewrite } from 'rewrite-imports'` for ESM or `const { rewrite } = require('rewrite-imports')` for CommonJS.","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'`.","error":"TypeError: rewrite-imports is not a function"},{"fix":"Review 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.","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).","error":"Transformed code contains syntax errors or unexpected runtime behavior related to module loading."}],"ecosystem":"npm"}