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.
Discussions

javascript - module.exports vs exports in Node.js - Stack Overflow
Notice that module.exports is the left most argument. Attaching properties to exports is not the same since you are not reassigning it. That is why this works ... It's a subtle difference to do with the way objects are passed by reference in JavaScript. More on stackoverflow.com
🌐 stackoverflow.com
What is "module.exports"?
When NodeJS came out in 2009, JavaScript didn't have a standard way of doing modules. So the Node team came up with their own way, they called it CommonJS . This is the module.exports and require syntax. Years later, in the ES2015 edition, the JavaScript standards committee finally came up with a standard way of doing modules in the browser and non-browser environments. These are called simply JavaScript modules or ECMAScript modules. This is the import and export syntax. Node can't just change to default to the standard modules because that would break existing projects. So if you want the standard modules you have to opt-in, either by configuring the package.json or using the .mjs file extension. More information is in the first link More on reddit.com
🌐 r/learnjavascript
4
3
October 23, 2022
module.exports best practices?
This is from the ES6 documentation ( http://exploringjs.com/es6/ch_modules.html#sec_overview-modules ): 16.1 Overview JavaScript has had modules for a long time. 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. These two ways can be mixed, but it is usually better to use them separately. 16.1.1 Multiple named exports There can be multiple named exports: //------ lib.js ------ export const sqrt = Math.sqrt; export function square(x) { return x * x; } export function diag(x, y) { return sqrt(square(x) + square(y)); } //------ main.js ------ import { square, diag } from 'lib'; console.log(square(11)); // 121 console.log(diag(4, 3)); // 5 You can also import the complete module: //------ main.js ------ import * as lib from 'lib'; console.log(lib.square(11)); // 121 console.log(lib.diag(4, 3)); // 5 16.1.2 Single default export There can be a single default export. For example, a function: //------ myFunc.js ------ export default function () { ··· } // no semicolon! //------ main1.js ------ import myFunc from 'myFunc'; myFunc(); Or a class: //------ MyClass.js ------ export default class { ··· } // no semicolon! //------ main2.js ------ import MyClass from 'MyClass'; const inst = new MyClass(); Note that there is no semicolon at the end if you default-export a function or a class (which are anonymous declarations). More on reddit.com
🌐 r/node
16
24
May 24, 2017
javascript - Difference between "module.exports" and "exports" in the CommonJs Module System - Stack Overflow
On this page (http://docs.nodejitsu.com/articles/getting-started/what-is-require), it states that "If you want to set the exports object to a function or a new object, you have to use the module. More on stackoverflow.com
🌐 stackoverflow.com
🌐
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
2 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.
🌐
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 - By module.exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() method.
🌐
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.
Top answer
1 of 9
743

module is a plain JavaScript object with an exports property. exports is a plain JavaScript variable that happens to be set to module.exports. At the end of your file, node.js will basically 'return' module.exports to the require function. A simplified way to view a JS file in Node could be this:

var module = { exports: {} };
var exports = module.exports;

// your code

return module.exports;

If you set a property on exports, like exports.a = 9;, the result is exactly the same as module.exports.a = 9. This is because objects are passed around as references in JavaScript and both exports and module.exports refer to the same object as long as you don't reassign one of them to a different object.

The latter is what happens if you set exports to something new. It will no longer point to the same object as module.exports, and since only module.exports is returned to require, whatever you set exports to is not.

2 of 9
84

Renee's answer is well explained. Addition to the answer with an example:

Node does a lot of things to your file and one of the important is WRAPPING your file. Inside nodejs source code "module.exports" is returned. Lets take a step back and understand the wrapper. Suppose you have

greet.js

var greet = function () {
   console.log('Hello World');
};

module.exports = greet;

the above code is wrapped as IIFE(Immediately Invoked Function Expression) inside nodejs source code as follows:

(function (exports, require, module, __filename, __dirname) { //add by node

      var greet = function () {
         console.log('Hello World');
      };

      module.exports = greet;

}).apply();                                                  //add by node

return module.exports;                                      //add by node

and the above function is invoked (.apply()) and returned module.exports. At this time module.exports and exports pointing to the same reference.

Now, imagine you re-write greet.js as

exports = function () {
   console.log('Hello World');
};
console.log(exports);
console.log(module.exports);

the output will be

[Function]
{}

the reason is : module.exports is an empty object. We did not set anything to module.exports rather we set exports = function()..... in new greet.js. So, module.exports is empty.

Technically exports and module.exports should point to same reference(thats correct!!). But we use "=" when assigning function().... to exports, which creates another object in the memory. So, module.exports and exports produce different results. When it comes to exports we can't override it.

Now, imagine you re-write (this is called Mutation) greet.js (referring to Renee answer) as

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

console.log(exports);
console.log(module.exports);

the output will be

{ a: [Function] }
{ a: [Function] }

As you can see module.exports and exports are pointing to same reference which is a function. If you set a property on exports then it will be set on module.exports because in JS, objects are pass by reference.

Conclusion is always use module.exports to avoid confusion. Hope this helps. Happy coding :)

🌐
webpack
webpack.js.org › guides › package-exports
Package exports | webpack
The exports field in the package.json of a package allows to declare which module should be used when using module requests like import "package" or import "package/sub/path". It replaces the default implementation that returns main field resp.