Registry / web-framework / babel-plugin-transform-require

babel-plugin-transform-require

JSON →
library1.1.0jsnpmunverified

A Babel plugin that transforms CommonJS require() calls into ES6 import statements. Version 1.1.0 is the latest stable release. It converts patterns like var a = require('a') to import a from 'a'. This plugin is useful for incremental migration of codebases from CommonJS to ES modules. It operates as a Babel plugin and requires babel-core (or @babel/core) to function. Unlike more comprehensive tools like @babel/plugin-transform-modules-commonjs, this plugin focuses solely on require-to-import conversion without handling exports. The package has a slow release cadence and is minimal in scope.

npm install babel-plugin-transform-require
INSTALL
IMPORT
SIG · BABEL-PLUGIN-TRANS
B
babel-plugin-transform-require
web-frameworkjavascriptv1.1.0
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 (transformRequire)
import transformRequire from 'babel-plugin-transform-require'
const transformRequire = require('babel-plugin-transform-require')
The package exports a default function. Using CommonJS require works in Node but may break in ESM-only environments.
Node require (CJS)
const transformRequire = require('babel-plugin-transform-require')
const { transformRequire } = require('babel-plugin-transform-require')
There is no named export; using destructuring will result in undefined.
In .babelrc plugins array
{"plugins": ["transform-require"]}
{"plugins": ["babel-plugin-transform-require"]}
Babel resolves plugins with the 'babel-plugin-' prefix automatically, but the full name also works.

Shows how to transform a require call to an import statement using the plugin programmatically with babel-core.

// Install: npm install --save-dev babel-core babel-plugin-transform-require import {transform} from 'babel-core'; import transformRequire from 'babel-plugin-transform-require'; const codeES5 = 'var a = require("a")'; const codeES6 = transform(codeES5, { plugins: [transformRequire] }).code; console.log(codeES6); // import a from "a";
Debug
Known issues
gotchaThe plugin only transforms require() calls. It does not handle module.exports or exports, so those remain as-is.
fix
Use additional plugins like @babel/plugin-transform-modules-commonjs for a full CJS-to-ESM conversion.
affects: >=1.0.0
gotchaThe plugin does not support dynamic require() patterns (e.g., var x = require(someVar)). Such calls are left unchanged.
fix
Manually refactor dynamic requires or use a runtime helper for dynamic imports.
affects: >=1.0.0
gotchaIf the require() result is assigned to a variable that is reassigned later, the import statement will not be semantically equivalent. E.g., var a = require('a'); a = 5; becomes import a from 'a'; a = 5; which is invalid.
fix
Manually adjust code to use let or const and ensure no reassignment of imported bindings.
affects: >=1.0.0
gotchaThe plugin relies on babel-core (or @babel/core) but does not declare it as a peer dependency. Users must install it separately.
fix
Run npm install --save-dev babel-core (or @babel/core for Babel 7+).
affects: >=1.0.0
deprecatedThe package is based on the deprecated babel-core API (Babel 6). It may not work with Babel 7+ without adjustments.
fix
Use a more modern plugin or migrate to @babel/plugin-transform-modules-commonjs.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'babel-core'
babel-core is not installed or is missing from node_modules.
fix
Run npm install --save-dev babel-core (or @babel/core) to install babel-core as a development dependency.
TypeError: transformRequire is not a function
The import is destructured incorrectly (e.g., { transformRequire }) but the package exports a default function.
fix
Use import transformRequire from 'babel-plugin-transform-require' without curly braces.
ReferenceError: require is not defined
Using require() in an ES module environment without proper bundler/transpiler setup.
fix
Use the plugin to transform the require calls, or ensure that Node.js is configured with --experimental-require-module flag if applicable.
SyntaxError: Unexpected token import
The transformed code contains import statements but the environment does not support ES modules.
fix
Use a bundler like Webpack or a Node.js version that supports ES modules with --experimental-modules flag.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources
babel-plugin-transform-require — npm install babel-plugin-transform-require · libregistry