Modules (CommonJS)
JavaScript · Reference cheat sheet
Modules (CommonJS)
JavaScript · Reference cheat sheet
📋 Overview
CommonJS (CJS) is Node’s classic module system: require, module.exports, and synchronous loading. ES modules (import/export) are the web standard and Node’s modern default — know CJS for legacy packages, interop, and tooling. Prefer ESM for new browser and Node code.
🔧 Core concepts
- Export:
module.exports = valueorexports.foo = ...(alias ofmodule.exportsinitially). - Import:
const x = require("./file")— cached after first load. - Resolution: core modules,
node_modules, relative paths; optional extensions. __filename/__dirname: file path helpers (not in ESM — useimport.meta.url).- Circular deps: partially filled
exportsduring cycle. - Interop: ESM can sometimes
importCJS; CJS loads ESM via dynamicimport().
// math.cjs
function add(a, b) {
return a + b;
}
module.exports = { add };💡 Examples
// Export patterns
module.exports = function main() {};
module.exports = { read, write };
exports.version = "1.0.0"; // OK if not reassigned module.exports
// BAD: breaks exports alias
exports = { a: 1 }; // does not change module.exports
// Import
const { add } = require("./math");
const path = require("node:path");
// Conditional / dynamic path
const lang = require(`./locales/${name}.js`); // still sync; path must be resolvable
// Cache bust (rare)
delete require.cache[require.resolve("./config")];
// ESM consuming CJS (typical bundler/Node)
import pkg from "./legacy.cjs";
import * as ns from "./legacy.cjs";
// CJS consuming ESM
async function load() {
const esm = await import("./modern.mjs");
return esm.default;
}
// __dirname in ESM
import { fileURLToPath } from "node:url";
import { dirname } from "node:path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);⚠️ Pitfalls
- Reassigning
exports = ...does not export — assignmodule.exports. requireis sync and blocks — avoid for huge optional deps at startup when dynamicimportfits.- Dual packages (CJS+ESM) can cause “duplicate instance” bugs — follow package
exportsmap. - Browser has no native
require— use ESM or a bundler. - Live bindings differ: CJS copies values on require; ESM exports are live.
🔗 Related
- import_export.md — ES modules
- oop.md — organizing code
- json.md — require JSON in Node
- error.md — MODULE_NOT_FOUND