Part 1: The "Revealing Module" Pattern (The OG Secret)
Before JavaScript had a official module system, developers used a trick called the Revealing Module Pattern. It uses an IIFE (Immediately Invoked Function Expression).
In JavaScript, variables inside a function stay inside that function. By returning only an object with specific functions, we "reveal" only what we want the world to see.
const mySecretModule = (() => {
const privatePin = "1234"; // Nobody can see this!
const getPin = () => console.log(privatePin);
return { getPin }; // We only reveal the function, not the variable
})();
Part 2: CommonJS (The Traditional Node Way)
When Node.js first started, it used CommonJS (CJS). You’ve probably seen this using require and module.exports.
require a file, Node.js wraps your code in a secret function (much like the IIFE above!) and gives it the exports object.require the same file twice, it gives you the exact same object from its memory (cache).Part 3: ES Modules (The Modern Standard)
ESM (ECMAScript Modules) is the official standard for JavaScript today. It uses import and export.
Key differences from CJS:
import at the very top of your file. You can't put it inside an if statement like you can with require.⚡ 2026 Updates (Modern Node.js Docs)
The book notes some things were experimental in 2020 that are standard now:
node: prefix for built-in modules (e.g., import fs from 'node:fs'). This makes it clear you aren't using a random npm package.await code in an async function at the top level of your file. You can just use await directly!