Even though the question has been answered and accepted long ago, I just want to share my 2 cents.

You can imagine that at the very beginning of your file there is something like (just for explanation):

var module = new Module(...);
var exports = module.exports;

So whatever you do just keep in mind that module.exports and NOT exports will be returned from your module when you're requiring that module from somewhere else.

So when you do something like:

exports.a = function() {
    console.log("a");
}
exports.b = function() {
    console.log("b");
}

You are adding 2 functions a and b to the object to which module.exports points, so the typeof the returning result will be an object : { a: [Function], b: [Function] }

Of course, this is the same result you will get if you are using module.exports in this example instead of exports.

This is the case where you want your module.exports to behave like a container of exported values. Whereas, if you only want to export a constructor function then there is something you should know about using module.exports or exports. Recall that module.exports will be returned when you require something, not exports.

module.exports = function Something() {
    console.log('bla bla');
}

Now typeof returning result is 'function' and you can require it and immediately invoke it like:
var x = require('./file1.js')(); because you overwrite the returning result to be a function.

However, using exports you can't use something like:

exports = function Something() {
    console.log('bla bla');
}
var x = require('./file1.js')(); //Error: require is not a function

Because with exports, the reference no longer points to the object where module.exports points, so there is not a relationship between exports and module.exports anymore. In this case module.exports still points to the empty object {} which will be returned.

The accepted answer from another topic should also help: Does JavaScript pass by reference?

Answer from Srle on Stack Overflow
🌐
SitePoint
sitepoint.com › blog › javascript › understanding module.exports and exports in node.js
Understanding module.exports and exports in Node.js — SitePoint
January 29, 2024 - module.exports and exports are specific to Node.js and are not available in browser JavaScript.
🌐
freeCodeCamp
freecodecamp.org › news › module-exports-how-to-export-in-node-js-and-javascript
module.exports – How to Export in Node.js and JavaScript
April 25, 2022 - The second log does not contain the value1 export anymore. It only has the function exported using module.exports. Why is this so? module.exports = ... is a way of reassigning a new object to the exports property.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › export
export - JavaScript | MDN
The export declaration is used to export values from a JavaScript module. Exported values can then be imported into other programs with the import declaration or dynamic import. The value of an imported binding is subject to change in the module that exports it — when a module updates the ...
🌐
Stackify
stackify.com › node-js-module-exports
Node.js Module Exports - Demystified - Stackify
July 8, 2024 - The creators of Node.js were keen for it to not suffer the same fate as the browser by having a big, dangerous global scope. So they implemented Node.js with a module specification called CommonJS (which is where you get the module exports and require syntax from). To make matters more complicated, though, you can also use the soon-to-be-implemented JavaScript module syntax.
Top answer
1 of 16
661

Even though the question has been answered and accepted long ago, I just want to share my 2 cents.

You can imagine that at the very beginning of your file there is something like (just for explanation):

var module = new Module(...);
var exports = module.exports;

So whatever you do just keep in mind that module.exports and NOT exports will be returned from your module when you're requiring that module from somewhere else.

So when you do something like:

exports.a = function() {
    console.log("a");
}
exports.b = function() {
    console.log("b");
}

You are adding 2 functions a and b to the object to which module.exports points, so the typeof the returning result will be an object : { a: [Function], b: [Function] }

Of course, this is the same result you will get if you are using module.exports in this example instead of exports.

This is the case where you want your module.exports to behave like a container of exported values. Whereas, if you only want to export a constructor function then there is something you should know about using module.exports or exports. Recall that module.exports will be returned when you require something, not exports.

module.exports = function Something() {
    console.log('bla bla');
}

Now typeof returning result is 'function' and you can require it and immediately invoke it like:
var x = require('./file1.js')(); because you overwrite the returning result to be a function.

However, using exports you can't use something like:

exports = function Something() {
    console.log('bla bla');
}
var x = require('./file1.js')(); //Error: require is not a function

Because with exports, the reference no longer points to the object where module.exports points, so there is not a relationship between exports and module.exports anymore. In this case module.exports still points to the empty object {} which will be returned.

The accepted answer from another topic should also help: Does JavaScript pass by reference?

2 of 16
462

Setting module.exports allows the database_module function to be called like a function when required. Simply setting exports wouldn't allow the function to be exported because node exports the object module.exports references. The following code wouldn't allow the user to call the function.

module.js

The following won't work.

exports = nano = function database_module(cfg) {return;}

The following will work if module.exports is set.

module.exports = exports = nano = function database_module(cfg) {return;}

console

var func = require('./module.js');
// the following line will **work** with module.exports
func();

Basically node.js doesn't export the object that exports currently references, but exports the properties of what exports originally references. Although Node.js does export the object module.exports references, allowing you to call it like a function.


2nd least important reason

They set both module.exports and exports to ensure exports isn't referencing the prior exported object. By setting both you use exports as a shorthand and avoid potential bugs later on down the road.

Using exports.prop = true instead of module.exports.prop = true saves characters and avoids confusion.

🌐
W3Schools
w3schools.com › js › js_modules.asp
JavaScript Modules
A JavaScript module is usually a file, but it can also be an HTML script. A module file is a .js file using import / export.
Find elsewhere
🌐
Reddit
reddit.com › r/learnjavascript › what is "module.exports"?
r/learnjavascript on Reddit: What is "module.exports"?
October 23, 2022 -

Im using MongoDB + Mongoose and you need to use:

const ExampleSchema = new mongoose.Schema({
  title: example_title
})

module.exports = mongoose.model("List", ExampleSchema);

What is module.exports? Why is it "exportS" instead of "export"? Is it a completely different thing than normal export? What happens if I did:

export default mongoose.model("List", ExampleSchema);

instead? I wouldn't know to use "module.exports" if I didn't come across it in a tutorial.

When are you supposed to use it? How do you know when it's necessary?

🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Modules
JavaScript modules - JavaScript | MDN
3 weeks ago - If you really value the clarity of using .mjs for modules versus using .js for "normal" JavaScript files, but don't want to run into the problem described above, you could always use .mjs during development and convert them to .js during your build step. ... Some tools may never support .mjs. The <script type="module"> attribute is used to denote when a module is being pointed to, as described in Applying the module to your HTML. The first thing you do to get access to module features is export them.
🌐
Vance Lucas
vancelucas.com › blog › module-exports-all-the-things
module.exports All The Things! - Vance Lucas
June 5, 2014 - One thing I find myself doing a lot in javascript files for node.js, or any other CommonJS-based system is typing module.exports quite a lot, or creating these obnoxiously large and repetitive module.exports declarations at the bottom of my files. For example in a helper library for a Titanium ...
🌐
Medium
medium.com › @devq › the-difference-between-module-export-and-export-default-740039fed547
The Difference between ‘module.export’ and ‘export default’ | by Dev_Q | Medium
November 21, 2023 - In summary, `module.exports` is associated with CommonJS modules used in Node.js, while `export default` is associated with ES6 modules used in modern JavaScript environments, including browsers and newer versions of Node.js.
🌐
TutorialsTeacher
tutorialsteacher.com › nodejs › nodejs-module-exports
Node.js Export Module
The module.exports is a special object which is included in every JavaScript file in the Node.js application by default. The module is a variable that represents the current module, and exports is an object that will be exposed as a module.
🌐
OneUptime
oneuptime.com › home › blog › how to use module.exports and require properly in node.js
How to Use module.exports and require Properly in Node.js
January 22, 2026 - Every Node.js file has access to a module object. The module.exports property determines what the file exports when other files require() it.
🌐
Medium
chanduthedev.medium.com › module-exports-vs-require-vs-import-vs-export-in-javascript-b9f2605876f1
module.exports vs require vs import vs export in JavaScript | by chanduthedev | Medium
November 26, 2023 - module.exports vs require vs import vs export in JavaScript In JavaScript, if we want to access functions from another file we have to use module.exports and require. This involves two steps. Step 1 …
🌐
W3Schools
w3schools.com › js › js_modules_export.asp
JavaScript Modules Export
A module uses the export keyword to share values with other files.
🌐
Builder.io
builder.io › blog › nodejs-module-exports-vs-exports
Node.js module.exports vs. exports: The Right Choice
October 10, 2023 - Now that we understand how objects work in JavaScript, let's relate it to module.exports and exports. In Node.js, module is a plain JavaScript object with an exports property. exports is a plain JavaScript variable that happens to be set to ...
🌐
Reddit
reddit.com › r/node › module.exports best practices?
module.exports best practices? : r/node
May 24, 2017 - However, they were implemented via libraries, not built into the language. ES6 is the first time that JavaScript has built-in modules. ES6 modules are stored in files. There is exactly one module per file and one file per module. You have two ways of exporting things from a module.
🌐
NestJS
docs.nestjs.com › techniques › configuration
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
🌐
GeeksforGeeks
geeksforgeeks.org › node.js › what-is-the-purpose-of-module-exports-in-node-js
What is the purpose of module.exports in node.js ? - GeeksforGeeks
July 23, 2025 - The module.exports is actually a property of the module object in node.js. module. Exports is the object that is returned to the require() call.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › modules
Export and Import
Modules that declare a single entity, e.g. a module user.js exports only class User.
🌐
Medium
medium.com › @k36226531 › exporting-modules-in-node-js-module-exports-vs-exports-ed2ec8bcb516
Exporting Modules in Node.js: module.exports vs exports | by Jake | Medium
December 1, 2024 - By using this method, both exports and module.exports continue to reference the same object, ensuring that all values are reliably exported. module.exports is the object that is actually exported.