A Babel 7 plugin that transforms CommonJS modules (require, module.exports, exports) into ES modules (import/export). Version 1.1.6 is the latest stable release. It aims to produce spec-compliant ESM code and throws on unsupported patterns like non-static require or require.extensions. It offers escape hatches (synchronousImport, exportsOnly) for controlled conversion. Unlike @babel/plugin-transform-modules-commonjs (which targets the opposite direction), this plugin converts CJS to ESM and is mainly used for building ESM-compatible bundles or migration.
npm install babel-plugin-transform-commonjsVerified import paths — ran on the pinned version, not inferred.
Installs the plugin, configures Babel, and transforms a CJS file with named exports.
Use 'synchronousImport: true' to convert non-static require to dynamic import (may produce invalid code outside bundler).
Refactor exports to use valid identifiers or access via default export.
Use 'exportsOnly' instead of 'onlyExports'.
Install @babel/core@^7 and ensure @babel/core is in devDependencies.
Ensure bundler (e.g., webpack) polyfills Node core modules, or use a bundler that supports them natively.