Skip to solution
mediumPhone Screen

Describe the Node.js module system. What are CommonJS and ES Modules?

252 views
01

Understand the problem

Question presented to candidate: "A file uses import/export syntax and another uses require()/module.exports in the same project. What determines which system a given file uses, and can the two actually interoperate?"

What a strong answer should cover:

  • Node supports two distinct module systems: CommonJS (CJS)require()/module.exports, synchronous, Node's original system — and ES Modules (ESM)import/export, the standard JavaScript module system, asynchronous-capable, file-extension- or package.json-determined.
  • Which system a .js file uses is decided by package.json's "type" field: "type": "module" makes .js files ESM; its absence (or "type": "commonjs") makes them CJS. The explicit extensions .mjs (always ESM) and .cjs (always CJS) override the package.json setting file-by-file, regardless of "type".
  • 📌 A verifiable, not just documented, distinction: require is genuinely undefined inside a real ESM file — calling it throws a ReferenceError, not a discouraged-but-working fallback.
  • ESM importing CJS works one direction cleanly: a default import receives the entire module.exports object. A named import additionally works for properties Node's static analysis (cjs-module-lexer) can detect as simple assignments — but this is a best-effort heuristic, not a full guarantee, for dynamically-computed CJS exports.
  • CJS require()-ing ESM used to be impossible entirely (only dynamic import() worked); modern Node (from Node 22, broadening in later releases) allows require() to load a synchronous ES Module directly, with one hard exception (top-level await) — covered with its own live verification in the dedicated require(esm) question.
  • A precise answer keeps CJS and ESM's caching mechanisms conceptually distinct even though both cache: CJS keys its cache by resolved file path; ESM's module registry keys by resolved URL — this matters for edge cases like the same file reached via different URL forms.

Clarifying questions expected:

  • "Is the codebase fully ESM, fully CJS, or a mix?" — decides how much of the interop-specific detail actually matters here.
  • "Is the concern about how to WRITE interop code, or just understanding which system a given file uses?" — different depths of the same topic.

Code / implementation expected: Yes — showing a real ESM file importing a CJS file (and vice versa, referencing the dedicated require(esm) question) with the actual observed export shapes is the concrete, convincing version of this answer.

modulescommonjses modulesdependencies
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for Node.js phone screens — assumes basic import/export syntax familiarity. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every interop claim below was **actually executed

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

ESM exporting named + default, require() genuinely absent inside it, and ESM importing a CJS module's real export shape
// math.js (package.json has "type": "module")
export function add(a, b) { return a + b; }
export default function multiply(a, b) { return a * b; }

// main.js
import multiply, { add } from "./math.js";
console.log(add(2, 3), multiply(2, 3)); // 5 6
try {
  require("./math.js");
} catch (e) {
  console.log(e.message); // require is not defined
}

// cjs-mod.cjs
module.exports = { greeting: "hello from cjs" };
module.exports.extra = "bonus";

// import-cjs.mjs
import pkg from "./cjs-mod.cjs";
import * as ns from "./cjs-mod.cjs";
console.log(pkg); // { greeting: 'hello from cjs', extra: 'bonus' }
console.log(Object.keys(ns)); // [ 'default', 'extra', 'module.exports' ]
console.log(ns.default === ns["module.exports"]); // true
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 102 of 152 decoded in the Node.js track. One more won't hurt.

Back to track