CommonJS is a JavaScript module system built around synchronous `require()` calls and exports attached to `module.exports` (also available as `exports`). It grew as a practical way to modularize JavaScript outside the browser, using familiar JavaScript concepts rather than new syntax. section 13.2
Its design has practical tradeoffs, especially when modules meet ESM:
- `require()` is dynamic: it can appear inside functions or conditionals and can use a runtime-built argument. CommonJS loading is synchronous. section 13.2.2
- CommonJS packages have been bundled for browser use. ESM can generally consume CommonJS, but loading ESM from CommonJS is trickier; exports are represented as one object, with the default export accessed through `.default`. section 13.0 section 13.4
- A CommonJS module cannot load an ESM module that uses top-level `await` with `require()`; ESM can consume CommonJS while using top-level `await`. section 13.2.2
// Require 1.js, 2.js, or 3.js at random
require(`./${Math.ceil(Math.random() * 3)}.js`);A runtime-built path illustrates how `require()` can select a module dynamically. section 13.2.2




