🔧 module.exports vs exports in Node.js: What’s the Difference? 🤔
Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to
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 tomodule.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
📂 Importing in Another File
// file: app.js
const add = require('./math');
console.log(add(5, 3)); // Output: 8
🧐 What’s Happening?
- We assigned
add
directly tomodule.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}!`;
};
📂 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!
🧐 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!';
};
📂 Importing in Another File
// file: app.js
const wrong = require('./wrong');
console.log(wrong); // Output: {} (empty object) 😱
❌ Why Doesn’t This Work?
-
exports
is just a shortcut tomodule.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! 💻🔥
...
🔧 Grok 3: AI Thông Minh Nhất Thế Giới
📈 28.6 Punkte
🔧 Programmierung
🔧 Có thể bạn chưa biết (Phần 1)
📈 28.6 Punkte
🔧 Programmierung
📰 Difference-in-Difference 101
📈 19.66 Punkte
🔧 AI Nachrichten
🔧 Java vs Node - What Is The Difference?
📈 16.31 Punkte
🔧 Programmierung