Skip to content

Instantly share code, notes, and snippets.

@larrybolt
Created March 5, 2017 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larrybolt/587cfaf51f4bc5b618cd0308f3114ea3 to your computer and use it in GitHub Desktop.
Save larrybolt/587cfaf51f4bc5b618cd0308f3114ea3 to your computer and use it in GitHub Desktop.
NodeJS require singletons
const common = require('./common')
module.exports.add = (e) => common.add(e)
module.exports.get = () => common.get()
const common = require('./common')
module.exports.add = (e) => common.add(e)
module.exports.get = () => common.get()
const c = [];
module.exports.add = (n) => c.push(n)
module.exports.get = () => c
// run this file with node
// require both a and b,
const a = require('./a');
const b = require('./b');
// both a and b require common,
a.add('a')
b.add('b')
console.log('a', a.get())
console.log('b', b.get())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment