CommonJS to ES2015 Modules Transform
raw JSON → 4.0.1 verified Sat Apr 25 auth: no javascript maintenance
Babel plugin that transforms CommonJS require/exports syntax into ES2015 import/export statements. Version 4.0.1 is stable but not actively maintained; the package saw its last release in 2017. It supports most popular CommonJS patterns, including those generated by Babel's own CJS transform. Unlike similar tools (cjs-to-es6, lebab), it operates as a Babel plugin, making it easy to integrate into existing Babel pipelines. The transformation is lossy in some edge cases (e.g., dynamic requires or module.exports reassignment) and does not handle all CJS idioms.
Common errors
error Module 'babel-plugin-transform-commonjs-es2015-modules' is not found ↓
cause Plugin not installed or Babel cannot resolve it.
fix
Run
npm install babel-plugin-transform-commonjs-es2015-modules error Error: Plugin transform-commonjs-es2015-modules specified in .babelrc is not a valid plugin ↓
cause Incorrect plugin name or typo.
fix
Use correct plugin name: 'transform-commonjs-es2015-modules' (without 'babel-plugin-' prefix).
error TypeError: Property left of AssignmentExpression expected node to be of a type ["Identifier", "Pattern"] but got "MemberExpression" ↓
cause Unsupported pattern like `module.exports.property = ...` with complex expressions.
fix
Simplify the assignment or refactor to avoid member expression on module.exports.
Warnings
gotcha Does not handle dynamic requires like `require('./' + name)`. The plugin will skip or incorrectly transform them. ↓
fix Manually refactor dynamic requires or use a different tool. Consider bundlers like Webpack that handle dynamic imports natively.
deprecated Package has not been updated since 2017 and may not work with modern Babel versions (v7+). Babel 6 is still supported but Babel 7 changed plugin API. ↓
fix Use with Babel 6 or check if compatible with Babel 7 via shim. For newer projects, consider alternatives like lebab or manual conversion.
gotcha `module.exports` reassignment to a non-identifier, e.g., `module.exports = function(){}`, is transformed to `export default function(){}` but this may break in some module systems (e.g., AMD). ↓
fix Review output for anonymous exports; consider using named function expressions.
gotcha Nested `require` calls inside functions are not hoisted to imports. For example, `function a() { var x = require('x'); }` stays as-is. ↓
fix Manually move requires to top level if ES module semantics are required.
Install
npm install babel-plugin-transform-commonjs-es2015-modules yarn add babel-plugin-transform-commonjs-es2015-modules pnpm add babel-plugin-transform-commonjs-es2015-modules Imports
- default wrong
const plugin = require('babel-plugin-transform-commonjs-es2015-modules')correctimport plugin from 'babel-plugin-transform-commonjs-es2015-modules' - plugin as string wrong
plugins: ['babel-plugin-transform-commonjs-es2015-modules']correctplugins: ['transform-commonjs-es2015-modules']
Quickstart
// .babelrc
{
"plugins": ["transform-commonjs-es2015-modules"]
}
// input.js (CJS)
var foo = require('bar');
module.exports = foo;
// Run: npx babel input.js --plugins transform-commonjs-es2015-modules
// Output:
import foo from 'bar';
export default foo;