I am Hack Sparrow
Captain of the Internets.

Node.js Module – exports vs module.exports

What is the difference between exports and module.exports in Node.js?

You must be familiar with the exports object in Node.js modules, using which you create functions in your modules like this (assume in a file named rocker.js):

exports.name = function() {
console.log('My name is Lemmy Kilmister');
};

which you call from another file thus:

var rocker = require('./rocker.js');
rocker.name(); // 'My name is Lemmy Kilmister'

But what the heck is module.exports? Is it even legal?

Here is an eye-opener - module.exports is the real deal. exports is just module.exports's little helper. Your module returns module.exports to the caller ultimately, not exports. All exports does is collect properties and attach them to module.exports IF module.exports doesn't have something on it already. If there's something attached to module.exports already, everything on exports is ignored.

Put the following in rocker.js:

module.exports = 'ROCK IT!';
exports.name = function() {
console.log('My name is Lemmy Kilmister');
};

And this in another file, and run it:

var rocker = require('./rocker.js');
rocker.name(); // TypeError: Object ROCK IT! has no method 'name'

The rocker module completely ignored exports.name, and returned a string 'ROCK IT!'. From that you probably realize that your modules don't always have to be 'module instances'. Your modules can be any legal JavaScript object - boolean, number, date, JSON, string, function, array, and so on. Your module is whatever you set module.exports to. If you don't set module.exports to anything explicitly, the properties of exports and attached to it and returned.

In this case, your module is a class:

module.exports = function(name, age) {
this.name = name;
this.age = age;
this.about = function() {
console.log(this.name +' is '+ this.age +' years old');
};
};

and you'd use it this way:

var Rocker = require('./rocker.js');
var r = new Rocker('Ozzy', 62);
r.about(); // Ozzy is 62 years old

In this case, your module is an array:

module.exports = ['Lemmy Kilmister', 'Ozzy Osbourne', 'Ronnie James Dio', 'Steven Tyler', 'Mick Jagger'];

and you may use it this way:

var rocker = require('./rocker.js');
console.log('Rockin in heaven: ' + rocker[2]); //Rockin in heaven: Ronnie James Dio

So you get the point now - if you want your module to be of a specific object type, use module.exports; if you want your module to be a typical module instance, use exports.

The result of attaching properties to module.exports is akin to attaching properties to exports. For example this:

module.exports.name = function() {
console.log('My name is Lemmy Kilmister');
};

does the same thing as:

exports.name = function() {
console.log('My name is Lemmy Kilmister');
};

But note that, they are not the same thing. As I said earlier module.exports is the real deal, exports is just its little helper. Having said that, exports is the recommended object unless you are planning to change the object type of your module from the traditional 'module instance' to something else.

I hope this post helped you understand the difference between exports and module.exports, and learn a bit more about how modules work in Node.js. Any questions, ping me in the comments.

17 Responses to “Node.js Module – exports vs module.exports”


  1. Awesome article. Really helped me to understand the difference between the two while playing on ExpressJS.

    Thanks!


  2. Great explanation. You can also use module.exports to set variables that will be global to all the exports functions (but not global to the application).

    Silly example:

    var db;

    module.exports = function(dbHandle) {
    db = dbHandle;

    return exports;
    }

    module.exports = function getFromDb() {
    db.getAllTheThings();
    }


  3. Oops, forgot to say in the above example that you’d need to pass the db handle when requiring.

    Example:

    dbHandle = openDbConnectionOfSomeKind();
    var dbTools = require(‘./dbtools’)(dbHandle);


  4. Good one! It helps!


  5. Well explained!!!


  6. excellent! it helps a lot …


  7. Great post! And loved the metal references \m/


  8. \m/


  9. I’m teaching myself node and just wanted to say thanks. This post really helped me understand the whole module thing and how I can start to break apart my code and store it in files that make sense rather than lumped all together in one giant file.


  10. IMHO this is a very poor explanation.

    You’ve completely missed the point that effectively at the top of the module you get `var exports = module.exports`, so the former is just a reference to the latter. There’s no “attachment” or “helper” – they’re the _exact same object_!

    If you subsequently overwrite `module.exports` by assigning it a new value then _of course_ the contents of `exports` will be ignored.


  11. Thanks! It is very helpful for me.


  12. @Ray, where am I doing `var exports = module.exports`?


  13. awesome and to the point explanation.. thanks! :)


  14. Thank you for this quick explanation. I had been pouring over samples in the node.js doc as well as the web. I’ve spent hours trying to break up my code into several modules.
    When I saw the “this” keyword used inside the modules.export, exporting is now plainly understood.


  15. Very concise! This was definitely helpful. Thanks!! :)


  16. very helpful.. Thanks a lot


  17. Very concise and helpful!!! Thanks a lot.

Make a Comment