{"id":19183,"library":"babel-plugin-transform-commonjs-es2015-modules","title":"CommonJS to ES2015 Modules Transform","description":"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.","status":"maintenance","version":"4.0.1","language":"javascript","source_language":"en","source_url":null,"tags":["javascript","babel-plugin","es2015","modules","export","import","module","exports","commonjs"],"install":[{"cmd":"npm install babel-plugin-transform-commonjs-es2015-modules","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-transform-commonjs-es2015-modules","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-transform-commonjs-es2015-modules","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Requires Babel core to run as a plugin; not a direct dependency but peer/runtime requirement.","package":"@babel/core","optional":true}],"imports":[{"note":"Babel plugins are typically used via string name in .babelrc, not imported directly. If importing programmatically, ESM import works as shown.","wrong":"const plugin = require('babel-plugin-transform-commonjs-es2015-modules')","symbol":"default","correct":"import plugin from 'babel-plugin-transform-commonjs-es2015-modules'"},{"note":"Babel resolves plugin names without the prefix; using full 'babel-plugin-...' is also valid but more verbose.","wrong":"plugins: ['babel-plugin-transform-commonjs-es2015-modules']","symbol":"plugin as string","correct":"plugins: ['transform-commonjs-es2015-modules']"}],"quickstart":{"code":"// .babelrc\n{\n  \"plugins\": [\"transform-commonjs-es2015-modules\"]\n}\n\n// input.js (CJS)\nvar foo = require('bar');\nmodule.exports = foo;\n\n// Run: npx babel input.js --plugins transform-commonjs-es2015-modules\n// Output:\nimport foo from 'bar';\nexport default foo;","lang":"javascript","description":"Shows basic configuration and transformation of CommonJS require/exports to ES module imports/exports."},"warnings":[{"fix":"Manually refactor dynamic requires or use a different tool. Consider bundlers like Webpack that handle dynamic imports natively.","message":"Does not handle dynamic requires like `require('./' + name)`. The plugin will skip or incorrectly transform them.","severity":"gotcha","affected_versions":"<=4.0.1"},{"fix":"Use with Babel 6 or check if compatible with Babel 7 via shim. For newer projects, consider alternatives like lebab or manual conversion.","message":"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.","severity":"deprecated","affected_versions":">=4.0.0"},{"fix":"Review output for anonymous exports; consider using named function expressions.","message":"`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).","severity":"gotcha","affected_versions":"<=4.0.1"},{"fix":"Manually move requires to top level if ES module semantics are required.","message":"Nested `require` calls inside functions are not hoisted to imports. For example, `function a() { var x = require('x'); }` stays as-is.","severity":"gotcha","affected_versions":"<=4.0.1"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run `npm install babel-plugin-transform-commonjs-es2015-modules`","cause":"Plugin not installed or Babel cannot resolve it.","error":"Module 'babel-plugin-transform-commonjs-es2015-modules' is not found"},{"fix":"Use correct plugin name: 'transform-commonjs-es2015-modules' (without 'babel-plugin-' prefix).","cause":"Incorrect plugin name or typo.","error":"Error: Plugin transform-commonjs-es2015-modules specified in .babelrc is not a valid plugin"},{"fix":"Simplify the assignment or refactor to avoid member expression on module.exports.","cause":"Unsupported pattern like `module.exports.property = ...` with complex expressions.","error":"TypeError: Property left of AssignmentExpression expected node to be of a type [\"Identifier\", \"Pattern\"] but got \"MemberExpression\""}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}