DEV Community

SOVANNARO
SOVANNARO

Posted on

module.exports vs exports in Node.js: What’s the Difference? πŸ€”

Hey awesome devs! πŸ‘‹ If you've been working with Node.js modules, you've probably seen both module.exports and exports. But wait… aren’t they the same? 🀯

Well, not exactly! Understanding the difference can save you from hours of debugging and make you a better Node.js developer. In this blog, we’ll break it down in a fun and simple way! πŸš€


πŸ“¦ What Are module.exports and exports?

Both module.exports and exports are used to export data from a module so that it can be used in other files. But there’s a key difference in how they work. Let’s dive in! πŸŠβ€β™‚οΈ

βœ… module.exports

  • This is the actual object that gets returned when you require() a module.
  • You can assign anything to module.exports (object, function, class, etc.).
  • Overwriting it completely replaces the exported object.

βœ… exports

  • exports is just a shortcut/reference to module.exports.
  • You can attach properties to exports, but you can’t overwrite it directly.

Confused? Don’t worry! Let’s look at some examples. πŸ˜ƒ


πŸš€ Example 1: Using module.exports

// file: math.js
function add(a, b) {
    return a + b;
}

module.exports = add; // Exporting a single function
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Importing in Another File

// file: app.js
const add = require('./math');

console.log(add(5, 3)); // Output: 8
Enter fullscreen mode Exit fullscreen mode

🧐 What’s Happening?

  • We assigned add directly to module.exports.
  • When we require('./math'), we get only the function.

πŸš€ Example 2: Using exports

// file: utils.js
exports.greet = function(name) {
    return `Hello, ${name}!`;
};

exports.farewell = function(name) {
    return `Goodbye, ${name}!`;
};
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Importing in Another File

// file: app.js
const utils = require('./utils');

console.log(utils.greet('Alice')); // Output: Hello, Alice!
console.log(utils.farewell('Bob')); // Output: Goodbye, Bob!
Enter fullscreen mode Exit fullscreen mode

🧐 What’s Happening?

  • We added properties to exports, instead of overwriting it.
  • When we require('./utils'), we get an object with multiple functions.

🚨 Common Mistake: Overwriting exports

// file: wrong.js
exports = function() {
    return 'This will not work!';
};
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Importing in Another File

// file: app.js
const wrong = require('./wrong');

console.log(wrong); // Output: {} (empty object) 😱
Enter fullscreen mode Exit fullscreen mode

❌ Why Doesn’t This Work?

  • exports is just a shortcut to module.exports.
  • When you do exports = ..., you break the reference!
  • Always use module.exports when exporting a single item.

🎯 When to Use What?

Scenario Use
Exporting a single function, class, or object module.exports = something;
Exporting multiple functions or properties exports.property = something;

πŸš€ Final Thoughts

Both module.exports and exports help us share code across files, but knowing their differences will save you from confusing bugs. Remember:

  • βœ… Use module.exports when exporting a single item.
  • βœ… Use exports to attach multiple properties.
  • ❌ Don’t assign directly to exportsβ€”it won’t work!

This is just the beginning! In the next article, we’ll explore ES Modulesβ€”stay tuned! 🎯

If you found this blog helpful, make sure to follow me on GitHub πŸ‘‰ github.com/sovannaro and drop a ⭐. Your support keeps me motivated to create more awesome content! πŸš€

Happy coding! πŸ’»πŸ”₯

Top comments (0)