liveBook · Manning

Module

As explained in JavaScript in Depth

A single JavaScript file treated as a unit of code that can be loaded and share code with other modules.

A module is one JavaScript file that a runtime loads as a unit and can connect to other files to share code. The same file can behave differently when treated as CommonJS or an ECMAScript module (ESM), and across runtimes; even the file extension can affect Node.js behavior. section 1.1

Table 1.1 Running the same code in five different runtimes yields six different results
Runtime
Variation
Results
Node.js
CommonJS
Hello
Node.js
ECMAScript module
elHo
l
Deno
ECMAScript module
elHlo
Bun
ECMAScript module
Helo
L
Chrome
CommonJS
Error! process.nextTick does not exit.
Cloudflare Workers
ECMAScript module
eHllo

Compare the outputs of one asynchronous-task puzzle across runtimes and module types: the results vary, and some environments reject APIs used by the script. section 1.1

How JavaScript in Depth covers it

How deep it goes
Defines modules and compares runtime behavior with a concrete puzzle, without detailing module-system internals.
Examples
Compares an asynchronous-task puzzle across Node.js, Deno, Bun, Chrome, and Cloudflare Workers.
What you can do after
Diagnose runtime-dependent behavior by checking the module system, runtime, version, and file extension.
How it connects
Connects modules to CommonJS, ECMAScript Modules, and JavaScript runtimes.

Also explained in

Compare how 44 books explain module →

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.