Skip to content

Instantly share code, notes, and snippets.

@danielkhan
Last active September 1, 2021 00:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielkhan/6248777 to your computer and use it in GitHub Desktop.
Save danielkhan/6248777 to your computer and use it in GitHub Desktop.
node.js module.exports and exports explained
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