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:
// math.js
const PI = 3.14;
function add(a, b) {
return a + b;
}
module.exports = { PI, add };// app.js
const math = require('./math');
console.log(math.add(2, 3)); // 5The 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
.jsfile 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
- Path resolution: Node looks for the file (
.js,.json,.node). - Reading the file: its contents are read.
- Wrapping: the code is placed inside a function (to create a scope).
- Execution: it runs, and the result is stored in
module.exports. - Caching: the next
require()reads the module from memory.
This makes modules fast and reusable.
7. CommonJS vs ES Modules (ESM)
| Criterion | CommonJS (CJS) | ES Modules (ESM) |
|---|---|---|
| Import | require() | import |
| Export | module.exports | export |
| Loading | Synchronous | Asynchronous |
| Data type | A copy of the value | A live binding |
| Support | Node.js (by default) | The modern JS standard |
| Extension | .js | .mjs or "type": "module" |
| Tree-shaking | No | Yes |
| Compatibility | Older packages | The 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:
const fs = require('fs');
module.exports = { readFile: fs.readFile };ES Modules:
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:
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
| Object | Purpose |
|---|---|
require() | Imports a module |
module.exports | The object exported from a module |
exports | A shorthand reference to module.exports |
__dirname | The path to the current module's folder |
__filename | The path to the current file |
module | Information about the current module |
11. An example, all together
// 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:
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 viarequire(). CommonJS is exactly what Node.js's module architecture is built on.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.