What is module.exports in Node.js?

Why Interviewers Ask This

Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Node.js basics — a prerequisite for any developer role.

Answer

module.exports is the object that is returned when another file require()s a Node.js module. By default, it is an empty object {}. You can export a single value (function, class, or primitive) by assigning directly: module.exports = function greet() { ... };. You can export multiple things by adding properties: module.exports.greet = greet; module.exports.farewell = farewell; or using shorthand exports.greet = greet; (note: exports is just a reference to module.exports, so you must not reassign exports itself). ES Modules (the modern alternative) use export and import syntax instead. CommonJS (require/module.exports) is still dominant in existing Node.js code, but ES Modules are supported natively in Node.js 12+ with .mjs extension or "type": "module" in package.json.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Node.js codebase.