What is the module.exports vs exports difference?

Beginner

Answer

  • module.exports: The actual object that gets returned when module is required
  • exports: A reference to module.exports, convenient for adding properties
// Method 1: Using exports (shorthand)
exports.add = (a, b) => a + b;
exports.subtract = (a, b) => a - b;

// Method 2: Using module.exports
module.exports = {
    add: (a, b) => a + b,
    subtract: (a, b) => a - b
};

// Method 3: Exporting a single function
module.exports = (a, b) => a + b;

// Wrong: This breaks the reference
exports = { add: (a, b) => a + b }; // Won't work