Course contentsShow
← JavaScript
Lesson 1438 of 2,72331. ModulesFree lesson

CommonJS: The Original Node.js Module System

Learn how CommonJS became the default module system in Node.js before ES modules existed.

CommonJS: The Original Node.js Module System

You'll learn how CommonJS became Node.js's original module system and why it was created before ES modules existed.

The Pre-ES Modules Era

When Node.js was created in 2009, JavaScript had no built-in module system. Browsers ran scripts in a global scope, and there was no standardized way to organize code into separate, reusable files. Node.js needed a solution immediately—it couldn't wait years for ECMAScript to standardize one.

Enter CommonJS

CommonJS was a community-driven specification created to bring modules to JavaScript environments outside the browser. Node.js adopted this pattern, making it the default module system for server-side JavaScript from the beginning.

The Core Mechanism

CommonJS uses two primary constructs you've likely encountered:

  • require() - loads other modules
  • module.exports - exposes values from a module
// math.js (CommonJS module)
function add(a, b) {
  return a + b;
}

module.exports = add;
// app.js
const add = require('./math.js');
console.log(add(2, 3)); // 5

Why It Mattered

CommonJS was synchronous and simple, perfect for Node.js's file-system-based environment. While browsers eventually needed asynchronous module loading, Node.js could afford to load files synchronously since they were local. This design choice made CommonJS straightforward but also incompatible with native browser module systems later on.

The Legacy

Even though ES modules are now the standard (and Node.js supports them), millions of npm packages still use CommonJS. Understanding it remains essential for working with Node.js codebases, legacy projects, and the vast ecosystem built before ES modules arrived.

Key Takeaway: CommonJS was Node.js's pragmatic solution to JavaScript's missing module system, using require() and module.exports to enable code organization years before ES modules became standard.