liveBook · Manning

CommonJS

As explained in JavaScript in Depth

A module system specification for JavaScript based on synchronous loading and the require/export model.

A filename can change observable behavior before any code changes. In Node.js, a plain .js file runs as CommonJS by default, while .mjs selects ECMAScript Modules. The runtime therefore changes both how the file is interpreted and how scheduled work is ordered. section 1.1 section 8.5.3

Listing 1.1 A friendly puzzle
function print(a) {
  process.stdout.write(a);
}
 
queueMicrotask(() => print('e'));
process.nextTick(() => print('H'));
setTimeout(() => print('l'), 0);
Promise.resolve().then(() => print('l'));
setImmediate(() => print('o\n'));

Use the small scheduling puzzle to observe CommonJS behavior in Node.js rather than treating the output order as language-neutral. section 1.1

CommonJS is Node.js’s original module system: a module is one JavaScript file, and the module system determines how the runtime loads files and lets them share code. ECMAScript Modules are newer and built into the language, but Node.js loads the two systems differently. section 1.1

The distinction reaches beyond module syntax. Node.js executes a CommonJS script as a macrotask, then drains the nextTick queue before the microtask queue. An ECMAScript module runs as a microtask, so the same queued operations can reverse their apparent order. section 8.5.3

The extension changes the result

A scheduling script produces B, A, A, B as .js under Node.js. Renaming it to .mjs without changing its code produces A, B, B, A, because the execution context changes from CommonJS to an ECMAScript module. section 8.5.3

CommonJS behavior is consequently runtime-sensitive. The same code behaves differently across Node.js, Deno, Bun, browsers, and Cloudflare Workers; globals such as process.nextTick and setImmediate are not uniformly available. section 1.1

queueMicrotask(() => {
  queueMicrotask(() => console.log('A'));
  process.nextTick(() => console.log('B'));
});
 
process.nextTick(() => {
  queueMicrotask(() => console.log('A'));
  process.nextTick(() => console.log('B'));
});

Trace nested nextTick and microtask scheduling to see why CommonJS queue precedence produces surprising output. section 8.5.3

How JavaScript in Depth covers it

How deep it goes
Explains file classification and queue-draining order, but does not detail require or export implementation.
Examples
Uses queueMicrotask, process.nextTick, timers, promises, and extension-based reruns to expose differing output orders.
What it assumes
Assumes JavaScript functions and asynchronous callbacks, while defining modules, runtimes, queues, and execution contexts.
What you can do after
Evaluate/understand how Node.js classification changes module loading and asynchronous execution order.
How it connects
Connects CommonJS with Node.js, ECMAScript Modules, macrotasks, microtasks, and the nextTick queue.

Also explained in

Compare how 5 books explain CommonJS →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.