CommonJS (CJS)
CommonJS (CJS) is a module system in which each file loads the code it depends on by calling require() and shares its own values by assigning them to module.exports.
CommonJS isn’t part of ECMAScript. It began in 2009 as a community project to standardize modules for JavaScript outside the browser, and Node.js adopted it as its original module system. Browsers don’t provide require(), so CommonJS code reaches them only through a bundler.
Node.js wraps each CommonJS file in a function that receives exports, require, module, __filename, and __dirname. Top-level declarations therefore stay in the file’s own scope. A require() call loads and runs a file synchronously the first time, caches the result, and returns the same module.exports value to every later caller:
Node.js runs .cjs files as CommonJS. A .js file is CommonJS too when its nearest package.json sets "type": "commonjs", or when the nearest package.json sets no type or doesn’t exist and the file contains no ES module syntax.
The two module systems interoperate. An ES module can import a CommonJS file and receives its module.exports value as the default export. Since Node.js 20.19 and 22.12, require() can also load an ES module, as long as its module graph doesn’t use top-level await.
Example
Say a checkout script loads a price-formatting helper. The helper logs a line when it runs:
price.cjs
console.log("Running price.cjs");
exports.formatPrice = (cents) => `$${(cents / 100).toFixed(2)}`;
The checkout script logs before it requires the helper, then requires it twice:
checkout.cjs
console.log("Starting checkout");
const price = require("./price.cjs");
const priceAgain = require("./price.cjs");
console.log(price.formatPrice(1999));
console.log(`Same object: ${price === priceAgain}`);
Running the script shows the helper loading in the middle of the checkout code:
$ node checkout.cjs
Starting checkout
Running price.cjs
$19.99
Same object: true
The first require() call paused the checkout script, ran price.cjs to the end, and returned its exports. The second call returned the cached object without running the file again, so its log line appears once.
A common mistake is to replace exports instead of adding properties to it:
tax.cjs
exports = { rate: 0.2 };
invoice.cjs
const tax = require("./tax.cjs");
console.log(tax);
Running the invoice script prints an empty object:
$ node invoice.cjs
{}
The assignment only redirected the local exports variable. The require() call still returned the original module.exports object, which stayed empty. Exporting a whole new value takes an assignment to module.exports itself.
Related Resources
Course
From Python Packages to JavaScript Modules: ESM and CommonJS
Organize larger JavaScript programs with ES modules, shared state, dynamic imports, and CommonJS while porting a Python heating digest.
For additional information on related topics, take a look at the following resources:
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 18, 2026