Skip to main content

What does the term "CommonJS" mean?

1. What CommonJS is

CommonJS (CJS) is a standard (specification) for organizing JavaScript code as modules, meant for running outside the browser (on the server).

In other words:

CommonJS is a set of rules for how JavaScript code should export and import values between files, so modules can work in a server environment (for example, Node.js).

2. Why CommonJS came about

In the early 2000s, JavaScript existed only in the browser, and it had no built-in module system, everything was written in one file, and variables easily clashed.

When JavaScript started being used outside the browser (for example, on the server), a universal standard was needed that would let developers:

  • split code into files (modules);
  • import/export functions and objects;
  • manage dependencies.

So in 2009, a group of developers proposed the CommonJS standard (then called "ServerJS").

3. The core idea of CommonJS

Every file is an isolated module. It can:

  • export values (module.exports);
  • import other modules (require()).

A CommonJS example:

javascript
// math.js const PI = 3.14; function add(a, b) { return a + b; } module.exports = { PI, add };
javascript
// app.js const math = require('./math'); console.log(math.add(2, 3)); // 5

The key pieces of CommonJS:

  • require(), the function used to import;
  • module.exports / exports, the object used to export;
  • every module runs once, then gets cached.

4. CommonJS isn't just Node.js

Although CommonJS is associated with Node.js specifically, it started as an open initiative. Node.js simply became the standard's first and most successful implementation.

Other platforms that support CommonJS:

  • Narwhal
  • RingoJS
  • The MongoDB shell (early versions)
  • Electron (the main process)

5. CommonJS in Node.js

Node.js implements CommonJS almost completely (with a few quirks):

  • every .js file is a module;

  • modules are isolated from each other;

  • on load, Node.js wraps them in an internal function:

    javascript
    (function (exports, require, module, __filename, __dirname) { // module code });

That provides:

  • a private scope;
  • access to __dirname, __filename;
  • the ability to use require() inside any file.

6. How loading CommonJS modules works

  1. Path resolution: Node looks for the file (.js, .json, .node).
  2. Reading the file: its contents are read.
  3. Wrapping: the code is placed inside a function (to create a scope).
  4. Execution: it runs, and the result is stored in module.exports.
  5. Caching: the next require() reads the module from memory.

This makes modules fast and reusable.

7. CommonJS vs ES Modules (ESM)

CriterionCommonJS (CJS)ES Modules (ESM)
Importrequire()import
Exportmodule.exportsexport
LoadingSynchronousAsynchronous
Data typeA copy of the valueA live binding
SupportNode.js (by default)The modern JS standard
Extension.js.mjs or "type": "module"
Tree-shakingNoYes
CompatibilityOlder packagesThe newer ES6+ standard

CommonJS is a great fit for server-side JS, while ESM is a great fit for browsers and modern Node.js projects.

8. An example of the code differences

CommonJS:

javascript
const fs = require('fs'); module.exports = { readFile: fs.readFile };

ES Modules:

javascript
import fs from 'fs'; export const readFile = fs.readFile;

9. Why CommonJS is synchronous

CommonJS was designed for a server environment, where:

  • files live on the local disk;
  • reading modules synchronously causes no performance problems.

So:

javascript
const math = require('./math');

→ runs at startup, with no need to wait for async operations.

In a browser, though, that would "freeze" the page, which is why module loading in ESM is asynchronous.

10. In short: CommonJS's main objects

ObjectPurpose
require()Imports a module
module.exportsThe object exported from a module
exportsA shorthand reference to module.exports
__dirnameThe path to the current module's folder
__filenameThe path to the current file
moduleInformation about the current module

11. An example, all together

javascript
// logger.js console.log('Module loaded:', __filename); module.exports.log = (msg) => { console.log(`[LOG]: ${msg}`); }; // app.js const logger = require('./logger'); logger.log('Hello, CommonJS!');

Output:

javascript
Module loaded: /path/logger.js [LOG]: Hello, CommonJS!

In one sentence

CommonJS is a JavaScript module-system standard where code is organized into separate files, exported via module.exports, and imported via require(). CommonJS is exactly what Node.js's module architecture is built on.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.