Understanding module.exports vs exports in Node.js (The Simple Way)
When building a Node.js backend, one of the most confusing topics for beginners is:
Understanding module.exports vs exports in Node.js (The Simple Way)

When building a Node.js backend, one of the most confusing topics for beginners is:
What is the difference between
exportsandmodule.exports?
At first glance, they look the same — but they behave differently under the hood.
Let’s break it down in a simple, practical way.
1. Node.js Module System Basics
Every file in Node.js is treated as a module.
That means:
- Each file has its own scope
- Nothing is shared automatically
- You must explicitly export and import code
To share code between files, Node.js uses:
module.exports
and
exports
2. exports.connectDB = ... (Named Export)
When you write:
exports.connectDB = async () => {
console.log("DB connected");
};
You are exporting an object property.
Internally, Node treats it like this:
module.exports = {
connectDB: async () => {}
};
How to import it
const { connectDB } = require("./db");
connectDB();
✅ What you get:
- An object with multiple functions
- You must destructure to access them
3. module.exports = connectDB (Direct Export)
Now compare this:
const connectDB = async () => {
console.log("DB connected");
};
module.exports = connectDB;
Here, you are exporting the function itself, not an object.
How to import it
const connectDB = require("./db");
connectDB();
✅ What you get:
- Direct function
- No destructuring needed
4. Common Beginner Mistake
This causes confusion:
Export:
exports.connectDB = async () => {};
Import:
const connectDB = require("./db");
connectDB(); // ❌ Error
Why?
Because the actual value is:
{
connectDB: function
}
So you must do:
const { connectDB } = require("./db");
5. When to use which?
✅ Use exports when:
You have multiple functions:
exports.register = ...
exports.login = ...
exports.logout = ...
Example: Controllers
✅ Use module.exports when:
You are exporting a single thing:
- Database connection
- Utility function
- Service class
Example:
module.exports = connectDB;
6. Real Project Example
db.js
const mongoose = require("mongoose");
const connectDB = async () => {
await mongoose.connect(process.env.MONGO_URI);
console.log("MongoDB connected");
};
module.exports = connectDB;
server.js
const connectDB = require("./db");
connectDB();
Clean and simple.
Final Thoughts
Understanding module exports is essential for building:
- Express APIs
- Authentication systems
- Scalable backend architecture
Once you master this, Node.js becomes much easier to structure and scale.
메타데이터
- post_id
- 4e86ab8cd072
- slug
- understanding-module-exports-vs-exports-in-node-js-the-simple-way-4e86ab8cd072
- url
- https://medium.com/@uyanhewagetr/understanding-module-exports-vs-exports-in-node-js-the-simple-way-4e86ab8cd072
- canonical_url
- https://medium.com/@uyanhewagetr/understanding-module-exports-vs-exports-in-node-js-the-simple-way-4e86ab8cd072
- author_url
- https://medium.com/@uyanhewagetr
- status
- ok
- fetched_at
- 2026-06-15 20:49:13