module / module.exports
The CommonJS module object whose exports define the public API of a file.
Syntax
module.exports = value / exports.name = value Returns
object — The current module record; module.exports is what require() returns.
Examples
// math.js (CommonJS)
function add(a, b) { return a + b; }
module.exports = { add };
// main.js
const { add } = require('./math');
console.log(add(2, 3));
Output
5
// Detect if run directly vs. required
if (require.main === module) {
console.log('run as a script');
}
Output
run as a script
Notes
Reassigning `module.exports` replaces the entire export; `exports.x`
only adds a property and breaks if you also reassign `exports`. The
ESM equivalent is `export`/`export default`; the `require.main ===
module` idiom maps to checking `import.meta.main` in newer Node.