Home » JavaScript » Node.js » Exports vs module.exports in NodeJs, Which one to use?

About Himansh Jain

Himansh Jain
Works as the technical leader of an ADF Development team, An active member of OTN Jdev/ADF Forum of Oracle Community. He has written more than 200 article about Oracle ADF and JDeveloper. Many of his articles got published in WebLogic community newsletter and Oracle ACE newsletter from time to time. Awarded with Oracle ACE title (♠️) in the year 2015 for his contribution in Oracle Technology Network.

Exports vs module.exports in NodeJs, Which one to use?

We know that when our code is run through the node it’s wrapped in a function expression. This enables us to access the module object which is a parameter passed to that function.

1
2
3
(function (exports, require, module, __filename, __dirname) {
    //module code
});

This function expression is then invoked with the following arguments:

1
fn(module.exports, require, module, filename, dirname);

So we can see that in beginning both exportsand module.exports point towards the same memory where the empty module.exports{} reside.

module exports

When we require a module using require("module_name"), we get module.exports in return and not exports. This must always be kept in mind.

So when we do something like following in our module sample_module.js:

1
2
3
4
5
6
//sample_module
export.prop1 = "This is property 1;
 
export.func1 = function(){
  console.log("This is function 1");
}

Property and a method is being added to the object which is also pointed towards by module.exports

module exports

Hence when we require sample_module:

1
2
3
4
//app.js
var sample = require("sample_module");
sample.prop1;   //This is property 1
sample.func1;   //logs "This is function 1"

Of course, this is the same result we’d have got had we used module.exports in sample_module instead of exports.

However, if we have to export a number instead of an object and we do

1
2
//sample_module
exports = 5;

The situation would be like

module exports

On trying to access this from app.js:

1
2
3
4
//app.js
var sample = require("sample_module");
typeof(sample); //object... not number
conosle.log(sample);    //will log an empty object

This is happening because what actually gets returned via require() function is module.exports; and in the above example, we’ve set exports = 5, which essentially doesn’t mutate the object being pointed towards by module.exports, but instead, assign a new memory location to exportsand set its value as 5;

This has nothing to do with node. In Javascript, objects are passed by “copy of a reference”. So when the wrapped module is executed, exportsgot a copy of reference of module.exports. So exportscan modify the content of module.exports via the reference it received. However, if exportstries to overwrite the reference(as in the above example), it will not alter module.exports but rather create a new memory location for exportsand add content to that.

So what actually needs to be done for the above code to be properly executed is:

1
2
//sample_module
module.exports = 5;
module exports
1
2
3
4
//app.js
var sample = require("sample_module");
typeof(sample); //number
conosle.log(sample); //5

My personal advice is to always use module.exports and forget that shorthand exportsexists. This will reduce your cognitive load of remembering these minute details and make life a lot more easier.

Published on Web Code Geeks with permission by Himansh Jain, partner at our WCG program. See the original article here: exports vs module.exports in NodeJs, Which one to use?

Opinions expressed by Web Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Web Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Building web apps with Node.js
2. HTML5 Programming Cookbook
3. CSS Programming Cookbook
4. AngularJS Programming Cookbook
5. jQuery Programming Cookbook
6. Bootstrap Programming Cookbook
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of