# Output Module Formats — Babel and SWC: JavaScript Transpilation and Build Speed

Source: https://www.skillbyai.com/en/babel-swc/bs-output-modules

> Choose between CommonJS, ES modules and UMD output and publish packages that support both formats.

## 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.

```json
// 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.
