Modern JavaScript codebases mix ESM and CommonJS during migration. Interviewers want you to explain static vs dynamic loading and interoperability pain points.
Common Interview Questions
Q: Key differences between ESM and CommonJS?
Answer: ESM uses import/export, is statically analyzable, supports tree-shaking, and loads asynchronously. CommonJS uses require/module.exports, loads synchronously at runtime, and is the legacy Node default.
Q: Can you require an ESM-only package?
Answer: Generally no from pure CJS without dynamic import(). Node allows import() in CJS files for async ESM loading. Dual packages ship both formats when needed.
Q: What is live binding in ESM?
Answer: ESM importers see updated exported let/const bindings. CommonJS exports a copy of primitive values at require time.
// counter.js
export let count = 0;
export function inc() { count++; }
// main.js
import { count, inc } from './counter.js';
inc();
console.log(count); // 1
Q: What does type: module in package.json do?
Answer: Treats .js files as ESM in Node. Without it, .js defaults to CommonJS unless using .mjs extension.
Key Points for Interviews
-
ESM enables static analysis and dead-code elimination
-
CJS
requireis synchronous — problematic in browser bundling -
import()is the dynamic, code-splitting friendly form -
Default vs named exports affect refactoring and auto-imports
-
Next.js and Vite assume ESM-first toolchains
