Modules, Runtime & Tooling
ESM, CommonJS & Module Interop
ES modules are the JavaScript standard module system, while CommonJS is Node's older module format. Interop depends on runtime rules, package metadata, file extensions, compiler options, and bundler behavior.
- ESM uses static import/export syntax
- CommonJS uses runtime require/module.exports
- Package type and file extension matter
- Default import interop is a footgun
- Libraries have harder compatibility choices than apps
| System | Syntax | Resolution/behavior |
|---|---|---|
| ESM | import/export | Standard, static, async-capable loading |
| CommonJS | require/module.exports | Node legacy, dynamic |
| Dual package | Exports ESM and CJS | Consumer compatibility, more complexity |
| Bundler output | Tool-generated | May hide runtime details until publishing |
// ESM
export function parse(input) {
return JSON.parse(input)
}
import { parse } from "./parse.js"
// CommonJS
module.exports = { parse }
const { parse } = require("./parse")