Skip to content

Instantly share code, notes, and snippets.

@unflores
Created November 10, 2017 10:56
Show Gist options
  • Save unflores/35d3122954774ff01e4eba64e4f3f27d to your computer and use it in GitHub Desktop.
Save unflores/35d3122954774ff01e4eba64e4f3f27d to your computer and use it in GitHub Desktop.
Once a singleton always a singleton
var Thing = require('./thing');
console.log('Include thing from another file');
console.log(Thing.thing, ":thing in included");
var Thing = require('./thing');
console.log(Thing.thing, ": thing in main");
Thing.setSomething();
console.log(Thing.thing, ":thing in main");
require('./included');
// Thing.thing gets cached after the first require
// As long as the data is set early on, all subsequent requires
// will have access to it.
module.exports = {
setSomething: function() {
console.log('set Thing.thing');
module.exports.thing = 5;
},
thing: undefined
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment