Last active
September 1, 2021 00:40
-
-
Save danielkhan/6248777 to your computer and use it in GitHub Desktop.
node.js module.exports and exports explained
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
function Person(name) { | |
this.name = name; | |
} | |
var john = new Person('John'); | |
// implicit done by node.js: | |
// exports = module.exports | |
exports.myperson = john; | |
// test = require('./lib/test'); | |
// console.log(test) gives { myperson: { name: 'John' } } | |
exports = john; | |
// test = require('./lib/test'); | |
// console.log(test) gives {} | |
// What we have done is overwriting the pointer to module.exports with another object. | |
// module.exports has nothing to do with exports anymore | |
module.exports = john; | |
// test = require('./lib/test'); | |
// console.log(test) gives { name: 'John' } | |
// This sets the whole module.exports object to our John-Object | |
// as everything in module.exports is exported | |
// (we are overriding only the property exports of module) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment