Last active
August 29, 2015 13:58
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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