JavaScript Modules: ESM vs CommonJS (import/export vs require)
An interview-ready guide to JavaScript modules: ESM vs CommonJS, named vs default exports, live bindings, top-level await, Node interop, and the TypeScript settings that make it all line up.
On this page
Modules are a classic interview topic because they sit at the intersection of:
- language features (exports/imports)
- runtime behavior (Node vs browser)
- build tools (bundlers, tree-shaking)
This post gives you a clean mental model and practical examples.
30‑second interview answer
ESM (import/export) is the modern JavaScript module system with static imports (enables tree-shaking), support for top-level await, and live bindings. CommonJS (require, module.exports) is the older Node.js system with dynamic loading and different interop rules. In interviews: prefer ESM for new code, but understand interop and runtime differences.
Key points
- ESM imports are static; CJS is runtime/dynamic.
- ESM has live bindings; CJS exports are values.
- Node supports both, but interop has gotchas.
- Bundlers love ESM (tree-shaking).
1) What problem do modules solve?
Before modules, JS files leaked into the global scope and dependency order was painful.
Modules give you:
- explicit imports/exports
- isolated scope by default
- reliable dependency graphs
2) Two module systems you’ll see
A) CommonJS (CJS)
- historically Node.js default
- uses
require()andmodule.exports
// math.cjs
function add(a, b) { return a + b; }
module.exports = { add };
// app.cjs
const { add } = require('./math.cjs');
console.log(add(1, 2));B) ECMAScript Modules (ESM)
- modern JavaScript standard
- uses
import/export
// math.mjs
export function add(a, b) { return a + b; }
// app.mjs
import { add } from './math.mjs';
console.log(add(1, 2));3) Named exports vs default exports
Named exports
export const PI = 3.14159;
export function area(r) { return PI * r * r; }
// import
import { PI, area } from './circle.js';Pros:
- refactor-friendly
- clearer API
- better tooling support
Default export
export default function area(r) { return Math.PI * r * r; }
import area from './circle.js';Pros:
- convenient for “one main thing” modules
Interview tip:
- prefer named exports for libraries
- default export is fine for single-purpose modules
4) The big conceptual difference: how values are bound
ESM has live bindings
This is the most important interview detail.
If a module exports a variable, imports see updates (because it’s a live binding).
// counter.js
export let count = 0;
export function inc() { count++; }
// app.js
import { count, inc } from './counter.js';
console.log(count); // 0
inc();
console.log(count); // 1In ESM, this works as expected.
CJS exports are more like a snapshot (object)
CJS exports are properties on module.exports.
You can mutate the exported object, but semantics differ.
5) Loading and execution order
ESM: static analysis (mostly)
ESM imports are (mostly) static:
- you can’t conditionally import with normal
importstatements - build tools can analyze dependency graphs
- enables tree-shaking
import x from './x.js'; // top-level, staticCJS: dynamic
CJS require() is just a function call.
if (flag) {
const x = require('./x');
}This can be useful but makes static analysis harder.
6) Node.js specifics (important in interviews)
How Node decides ESM vs CJS
Node uses:
- file extension:
.mjs= ESM,.cjs= CJS - or
package.json:
{
"type": "module"
}If type is module, then .js files are treated as ESM.
If not, .js defaults to CJS.
7) Interop (mixing ESM and CJS)
This comes up a lot in real apps.
Importing CJS from ESM
import pkg from 'some-cjs-package';In Node, the default import of a CommonJS module is always its module.exports object, so this works reliably. Named imports (import { thing } from 'some-cjs-package') only work when Node's static analysis can detect that export in the CommonJS source. When it can't, you get "does not provide an export named 'thing'". The fix is to use the default import and read the property from it.
Requiring ESM from CJS
Older Node versions could only load ESM from CommonJS with an async dynamic import:
(async () => {
const mod = await import('./esm-module.js');
})();Current Node releases (20.19+ and 22.12+) can also require() an ES module synchronously, as long as the module (and its imports) doesn't use top-level await.
Interview answer:
- ESM → CJS: the default import is
module.exports; named imports depend on what Node can detect. - CJS → ESM:
require()works in current Node for ESM without top-level await. Otherwise useimport().
8) Top-level await (ESM feature)
ESM supports top-level await in modern runtimes:
const res = await fetch(url);
export const data = await res.json();This is not available in CJS the same way.
9) Bundlers, tree-shaking, and why ESM wins for frontend
Frontend bundlers (Vite/Webpack/Rollup) can tree-shake ESM better because imports are static.
Tree-shaking benefit:
- smaller bundles
- faster load
If you build libraries for the web, ESM is usually the better format.
10) TypeScript: three layers that must agree
TypeScript adds one more place for module bugs to hide. Three layers have to agree:
- Source syntax: what you write (
import/exportorrequire/module.exports) - Emit: what TypeScript (or your bundler) outputs
- Loader: what actually runs the output (Node's ESM or CJS loader, Vite, webpack)
TypeScript type-checks either style, but whether the code runs depends on the emit and the loader. Common symptoms when they disagree:
- "Cannot use import statement outside a module" means ESM output is being run by the CJS loader.
- "require is not defined" means CJS output is being run as ESM.
- A default import that is
undefinedat runtime usually means CJS output importing a CJS package withoutesModuleInterop.
The two flags everyone confuses
allowSyntheticDefaultImportsaffects type-checking only. It lets you writeimport x from "cjs-lib"when the typings don't declare a default, and it changes nothing at runtime.esModuleInteropchanges the emitted code when TypeScript outputs CommonJS. It wraps imports in helpers soimport express from "express"gets themodule.exportsfunction instead of a missing.default. It also turns onallowSyntheticDefaultImports.
If you emit real ESM ("module": "nodenext" with "type": "module"), esModuleInterop has no emit effect. Node's loader does the interop, and as above, the default import of a CJS package is its module.exports.
A team policy that prevents most module bugs
- Pick one runtime format for each package, then set
moduleandmoduleResolutionto match it (nodenextfor Node,bundlerfor Vite or webpack apps). Don't type-check withbundlerlocally and then run plain Node in production. - Turn on
verbatimModuleSyntax, so what you write is what gets emitted and type-only imports are marked withimport type. - With CJS output, keep
esModuleInteropon. - Use named imports for ESM libraries and the default import for CJS libraries, unless the library documents something else.
11) Interview Q&A
Q: ESM vs CJS — main differences?
- Syntax:
import/exportvsrequire/module.exports - ESM is static + supports live bindings + top-level await
- CJS is dynamic and synchronous
Q: Why is ESM better for tree-shaking?
- Static imports allow bundlers to see what’s used.
Q: What does “live binding” mean?
- Imports reflect updates to exported variables.
Q: How do you use ESM in Node?
.mjsorpackage.jsontype: module.
Q: Does TypeScript decide whether code is ESM or CJS?
- It controls what gets emitted, but the runtime (Node's loader or the bundler) decides how it loads.
Q: What does esModuleInterop do?
- When emitting CommonJS, it adds helpers so default imports from CJS packages get
module.exports.allowSyntheticDefaultImportsonly changes type-checking.
Q: Why can code compile but fail at runtime?
- The types are satisfied, but the emit and the loader disagree about the module format or the export shape.
Summary checklist
- I can explain ESM vs CJS in one minute.
- I know why ESM enables tree-shaking.
- I can explain live bindings.
- I know Node’s
.mjs/.cjs+type: modulerules. - I can explain
esModuleInteropvsallowSyntheticDefaultImports.
Summary
- Know the syntax differences.
- Know the live binding vs object export mental model.
- Know Node’s rules (
.mjs/.cjs,type: module). - For frontend + bundlers, ESM is the modern default.
Looking for structured, offline interview prep?
Free online lessons • Portable Amazon Kindle / Paperback
Frontend Interview Guide (2025 Edition)
By Rajesh Dhiman
Comprehensive preparation covering React internals, Web Performance, CSS architecture, and system design questions.
Frontend Interview Guide (2025 Edition)
By Rajesh Dhiman
Comprehensive preparation covering React internals, Web Performance, CSS architecture, and system design questions.
Note: All lessons on Frontend Interview remain completely free to read online. As an Amazon Associate I earn from qualifying purchases.
Written by Rajesh Dhiman
Authored by Rajesh Dhiman and engineered at Eunix Tech. Focused on deep-dive frontend engineering, JavaScript runtime internals, React architecture, and web performance.