Hint
ESM: static import/export, live bindings, always strict; CJS: require(), dynamic, copied values
// ββ ES Modules (ESM) βββββββββββββββββββββββββββββ
// math.js
export const PI = 3.14;
export function add(a, b) { return a + b; }
export default class App {}
// main.js
import { PI, add } from './math.js'; // named imports
import App from './App.js'; // default import
import * as Math from './math.js'; // namespace
// ββ CommonJS (CJS) βββββββββββββββββββββββββββββ
module.exports = { PI, add };
const { PI, add } = require('./math');
// ββ Key Differences ββββββββββββββββββββββββββββ
| Feature | ESM | CommonJS |
|---|---|---|
| Analysis | Static (parse time) | Dynamic (runtime) |
| Binding | Live (tracks changes) | Copied (snapshot) |
| Strict mode | Always | Opt-in |
| Top-level await | β | β |
| Browser | type="module" | Bundler needed |
| Tree shaking | β (static) | β (dynamic) |