intermediate
ESM and CommonJS
Static imports, live bindings, require, exports, interop, and package boundary behavior.
ESM uses static `import`/`export` with live bindings — importers observe updates from the exporter. CommonJS uses runtime `require` and `module.exports`.
`"type": "module"` in package.json, `exports` maps, and tool interop rules determine default import shapes. Circular dependencies can expose partially initialized modules.
ESM enables static analysis and tree shaking; CJS is more dynamic.
On interviews, explain the concept with a concrete example and name the runtime behavior interviewers probe.
Common pitfalls include mixing similar APIs and forgetting edge cases during live coding.
The trade-off is often clarity versus performance or safety versus convenience.
Checklist:
- Know static versus runtime loading.
- Check package type and exports field.
- Treat default import interop as tool-specific.