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! π»π₯
Top comments (0)