Skip to content

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.

Rajesh DhimanFebruary 08, 2026~20 min
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() and module.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); // 1

In 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 import statements
  • build tools can analyze dependency graphs
  • enables tree-shaking
import x from './x.js'; // top-level, static

CJS: 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 use import().

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:

  1. Source syntax: what you write (import/export or require/module.exports)
  2. Emit: what TypeScript (or your bundler) outputs
  3. 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 undefined at runtime usually means CJS output importing a CJS package without esModuleInterop.

The two flags everyone confuses

  • allowSyntheticDefaultImports affects type-checking only. It lets you write import x from "cjs-lib" when the typings don't declare a default, and it changes nothing at runtime.
  • esModuleInterop changes the emitted code when TypeScript outputs CommonJS. It wraps imports in helpers so import express from "express" gets the module.exports function instead of a missing .default. It also turns on allowSyntheticDefaultImports.

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 module and moduleResolution to match it (nodenext for Node, bundler for Vite or webpack apps). Don't type-check with bundler locally and then run plain Node in production.
  • Turn on verbatimModuleSyntax, so what you write is what gets emitted and type-only imports are marked with import type.
  • With CJS output, keep esModuleInterop on.
  • 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/export vs require/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?

  • .mjs or package.json type: 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. allowSyntheticDefaultImports only 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: module rules.
  • I can explain esModuleInterop vs allowSyntheticDefaultImports.

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.
Companion Book

Looking for structured, offline interview prep?

Free online lessons • Portable Amazon Kindle / Paperback

Frontend Interview Guide (2025 Edition) cover
Kindle edition

Frontend Interview Guide (2025 Edition)

By Rajesh Dhiman

Comprehensive preparation covering React internals, Web Performance, CSS architecture, and system design questions.

View on Amazon ↗Kindle & Paperback
Frontend Interview Guide (2025 Edition) cover
Paperback edition

Frontend Interview Guide (2025 Edition)

By Rajesh Dhiman

Comprehensive preparation covering React internals, Web Performance, CSS architecture, and system design questions.

View on Amazon.com ↗Not available on Amazon.in. Readers in India: choose Kindle.

Note: All lessons on Frontend Interview remain completely free to read online. As an Amazon Associate I earn from qualifying purchases.

RD

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.