Skip to content

Instantly share code, notes, and snippets.

@matths
Last active August 29, 2015 13:58
Show Gist options
  • Save matths/10380868 to your computer and use it in GitHub Desktop.
Save matths/10380868 to your computer and use it in GitHub Desktop.
When beginning with node.js and modules, it's always difficult to understand the difference between exports and module.exports. Exports is a local reference to an empty plain object ({}), where module.exports is also referring to. But module.exports is what is assigned to the return value of the require() method, not exports. Changing exports to…
exports {}
module.exports {}
module.exports == exports YES
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
exports {}
module.exports function myExport() { console.log('an exported function instead of a plain object'); }
module.exports == exports NO
an exported function instead of a plain object
console.log("exports", exports);
console.log("module.exports", module.exports);
console.log("module.exports == exports", (module.exports==exports?"YES":"NO"));
console.log("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
module.exports = function myExport() { console.log('an exported function instead of a plain object'); };
console.log("exports", exports);
console.log("module.exports", module.exports);
console.log("module.exports == exports", (module.exports==exports?"YES":"NO"));
// var test = require("./test"); test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment