CommonJS basics — require/module.exports and exports
Learn CommonJS: require(), module.exports, exports shortcut, and a small interop taste with ESM.
CJS: big picture
Goal: Read and write small CommonJS modules.
- require() loads modules
- module.exports is the export object
- exports is a shortcut to the same object
- Quick ESM interop taste

require() — idea
require(path) returns the module's exported object in CommonJS.
// CommonJS import uses require()
const cjsExample = [
"const math = require(\\"./math.js\\");",
"console.log(math.sum(2, 3));"
];
console.log("require() example:");
for (const line of cjsExample) console.log(line);
// Emulate a loaded module object
const loaded = { sum: function (a, b) { return a + b; } };
console.log("emulated require sum:", loaded.sum(2, 3));
All lessons in this course
- ESM basics — import/export, default vs named
- CommonJS basics — require/module.exports and exports
- Module resolution in Node and browser bundlers