Skip to content

Instantly share code, notes, and snippets.

@marschhuynh
Last active September 2, 2016 05:49
Show Gist options
  • Save marschhuynh/dbf1101ff237239b2e5939b9a5e1189e to your computer and use it in GitHub Desktop.
Save marschhuynh/dbf1101ff237239b2e5939b9a5e1189e to your computer and use it in GitHub Desktop.
Different between Module.exports and exports in Nodejs

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;, that will set module.exports.a as well because objects are passed around as references in JavaScript, which means that if you set multiple variables to the same object, they are all the same object; so then exports and module.exports are the same object.

But if you set exports to something new, it will no longer be set to module.exports, so exports and module.exports are no longer the same object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment