Lädt...

🔧 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 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

📂 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 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}!`;
};

📂 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 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! 💻🔥

...

🔧 module.exports vs exports in Node.js: What’s the Difference? 🤔


📈 52.63 Punkte
🔧 Programmierung

🔧 Understanding module.exports and exports in Node.js


📈 42.8 Punkte
🔧 Programmierung

🔧 Bootcamping 02: Named exports and default exports - does it really matter?


📈 29.47 Punkte
🔧 Programmierung

🔧 Grok 3: AI Thông Minh Nhất Thế Giới


📈 28.6 Punkte
🔧 Programmierung

🕵️ Kèo Thẻ Phạt Vip66 Là Gì? 3 Lối Đánh Kèo Chậm Mà Chắc


📈 28.6 Punkte
🕵️ Reverse Engineering

🔧 KISS Principle: Giữ Mọi Thứ Đơn Giản Nhất Có Thể


📈 28.6 Punkte
🔧 Programmierung

🔧 Có thể bạn chưa biết (Phần 1)


📈 28.6 Punkte
🔧 Programmierung

🔧 Module Exports in Node.js: Unlocking the Power of Reusable Code 🚀


📈 28.07 Punkte
🔧 Programmierung

🔧 Difference between Module and Package in Node.js


📈 23.16 Punkte
🔧 Programmierung

🕵️ CVE-2022-21129 | nemo-appium 0.0.9 module.exports.setup command injection


📈 21.58 Punkte
🕵️ Sicherheitslücken

🔧 TS1315: Global module exports may only appear in declaration files


📈 21.58 Punkte
🔧 Programmierung

🕵️ CVE-2023-26127 | n158 module.exports command injection


📈 21.58 Punkte
🕵️ Sicherheitslücken

🕵️ Huawei NGFW Module/IPS Module SIP Module SIP Message denial of service


📈 20.54 Punkte
🕵️ Sicherheitslücken

🕵️ Huawei NGFW Module/IPS Module SIP Module SIP Message Denial of Service


📈 20.54 Punkte
🕵️ Sicherheitslücken

🕵️ Huawei NGFW Module/IPS Module COPS Module Message Pufferüberlauf


📈 20.54 Punkte
🕵️ Sicherheitslücken

🕵️ Huawei NGFW Module/IPS Module SIP Module SIP Message denial of service


📈 20.54 Punkte
🕵️ Sicherheitslücken

🕵️ Huawei NGFW Module/IPS Module SIP Module SIP Message denial of service


📈 20.54 Punkte
🕵️ Sicherheitslücken

🕵️ Huawei NGFW Module/IPS Module COPS Module Message memory corruption


📈 20.54 Punkte
🕵️ Sicherheitslücken

🕵️ atom-node-module-installer on Node.js Download weak encryption


📈 19.82 Punkte
🕵️ Sicherheitslücken

🕵️ node-printer Module up to 0.0.1 on Node.js lib/printer.js printDirect privilege escalation


📈 19.82 Punkte
🕵️ Sicherheitslücken

🕵️ atom-node-module-installer auf Node.js Download schwache Verschlüsselung


📈 19.82 Punkte
🕵️ Sicherheitslücken

🕵️ node-printer Module bis 0.0.1 auf Node.js lib/printer.js printDirect erweiterte Rechte


📈 19.82 Punkte
🕵️ Sicherheitslücken

📰 Difference-in-Difference 101


📈 19.66 Punkte
🔧 AI Nachrichten

📰 Deep web vs. dark web: Is there a difference, what does the difference affect?


📈 19.66 Punkte
📰 IT Security Nachrichten

🔧 Getting Started with Node.js: Understanding Node, npm, nvm, and npx (and How to Install Node.js)


📈 19.46 Punkte
🔧 Programmierung

🔧 Comparing Browser and Node.js: What's the Difference? 🤔


📈 16.31 Punkte
🔧 Programmierung

🔧 Node.js vs. Express.js: Understanding the Difference and Their Roles in Web Development


📈 16.31 Punkte
🔧 Programmierung

🔧 "Node.js: Understanding the Difference Between Current and LTS Versions"


📈 16.31 Punkte
🔧 Programmierung

🔧 Java vs Node - What Is The Difference?


📈 16.31 Punkte
🔧 Programmierung

matomo