Lesson 15 / 30
Output Module Formats
Choose between CommonJS, ES modules, and UMD output.
Module systems: CJS vs ESM
CommonJS (require, module.exports) dominated Node.js. ES modules (import, export) are the modern standard. Babel can output either or both.
Controlling output format
By default, Babel preserves module syntax. Use plugins to transform import to require or vice versa.
// Keep ES modules (default for modern builds)
{
"presets": ["@babel/preset-env"]
}
// Transform to CommonJS (for Node libraries)
{
"presets": [
["@babel/preset-env", { "modules": "commonjs" }]
]
}Libraries often ship both CJS and ESM
npm packages often include both formats: dist/index.js (CommonJS) and dist/index.mjs (ES modules). Bundlers and Node.js choose the right one.