require() do in CommonJS?require() loads and runs a module file, returning the exported values from that module.
You assign it to module.exports. For example: module.exports = myFunction;
exports and module.exports?exports is a shortcut to module.exports. Reassigning exports breaks the link, so only module.exports controls what is exported.
require() in CommonJS?Yes, require() can load JSON files and returns the parsed object.
require() multiple times for the same module?The module is loaded once and cached. Subsequent require() calls return the cached exports, not re-running the module code.
sayHello in CommonJS?Only module.exports = sayHello; correctly exports the function. Reassigning exports breaks the export link.
require('./math') return if math.js uses module.exports = { add, subtract }?The require() call returns the object assigned to module.exports, which contains the add and subtract functions.
Assigning an object with multiple properties to module.exports is the standard way to export multiple values.
exports = { a: 1 } instead of module.exports = { a: 1 }?Reassigning exports breaks the link to module.exports, so the exported object stays as the default empty object.
CommonJS returns a partially loaded module object during circular dependencies to avoid infinite loops.
require() and module.exports work together in CommonJS modules.exports and module.exports and why it matters.