Suggest an editImprove this articleRefine the answer for “What does the term "CommonJS" mean?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**CommonJS** is a module-system spec for JS outside the browser (from 2009), where a module file exports via `module.exports` and imports via `require()`; it's exactly what Node.js's module architecture is built on. **Key point:** CommonJS is synchronous by design, because files are read from local disk - that wouldn't work in a browser, which is why asynchronous ES Modules took hold there instead.Shown above the full answer for quick recall.Answer (EN)Image## 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) | 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: ```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 | 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 ```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.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.