babel-plugin-transform-require
raw JSON → 1.1.0 verified Sat Apr 25 auth: no javascript
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.
Common errors
error Error: Cannot find module 'babel-core' ↓
cause 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.
error TypeError: transformRequire is not a function ↓
cause 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.
error ReferenceError: require is not defined ↓
cause 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.
error SyntaxError: Unexpected token import ↓
cause 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.
Warnings
gotcha The 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.
gotcha The 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.
gotcha If 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.
gotcha The 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+).
deprecated The 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.
Install
npm install babel-plugin-transform-require yarn add babel-plugin-transform-require pnpm add babel-plugin-transform-require Imports
- default (transformRequire) wrong
const transformRequire = require('babel-plugin-transform-require')correctimport transformRequire from 'babel-plugin-transform-require' - Node require (CJS) wrong
const { transformRequire } = require('babel-plugin-transform-require')correctconst transformRequire = require('babel-plugin-transform-require') - In .babelrc plugins array wrong
{"plugins": ["babel-plugin-transform-require"]}correct{"plugins": ["transform-require"]}
Quickstart
// 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";