Interview frequency: …
Short answer
CJS is synchronous and uses require() at runtime, while ESM uses static import/export and resolves the dependency graph asynchronously before execution.
Explanation
CJS is Node.js's historical module system, where modules are loaded synchronously. ESM is the language's official standard, with static analysis and support for Top-level await.
Code example
export const add = (a: number, b: number) => a + b;Common mistakes
- Trying to use require() inside ESM files without creating a custom require with createRequire.
Follow-up questions
- How does Node.js handle circular dependencies between CJS and ESM?
- What is the Dual Package Hazard when publishing a library that supports both CJS and ESM in Node.js?