Skip to content

Instantly share code, notes, and snippets.

@jqtrde
Created May 5, 2012 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jqtrde/2603932 to your computer and use it in GitHub Desktop.
Save jqtrde/2603932 to your computer and use it in GitHub Desktop.
exports vs. module.exports in Node

Exports vs. Module.exports

Exports is a helper that actually points to Module.exports. Exports is the recommended way to go, unless you need to chance the Object type of Module.exports. So generally:

Bad: Module.exports = {} Good: Exports.name = function(){}

// In the real world, this would be saved as it's own file.
exports.name = function() {
console.log('Hi!')
}
// In a separate file, you would call the above.
var thing = require('./thing');
thing.name(); // Hi!
module.exports = 'Derp'
exports.name = function() {
console.log('Hi!');
}
// Calling the above...
var thing = require('./thing');
thing.name(); // TypeError: Object 'Derp' has no method '.name'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment