Skip to content

Instantly share code, notes, and snippets.

@jacekk
Last active January 12, 2018 22:21
Show Gist options
  • Save jacekk/4633fea92482101782e37b4d54f32e8a to your computer and use it in GitHub Desktop.
Save jacekk/4633fea92482101782e37b4d54f32e8a to your computer and use it in GitHub Desktop.
nodeJS imports and singletons
$ node ./test-imports.js
Ghost created
Ghost says: x
Ghost says: a
Ghost says: b
Ghost says: c
const proxied = require('./imported');
proxied.says('x');
module.exports = proxied;
function Ghost() {
console.log('Ghost created');
}
Ghost.prototype.says = function(word) {
console.log(`Ghost says: ${word}`);
}
module.exports = new Ghost();
const a = require('./imported');
const b = require('./import-proxy');
const c = require('./imported');
a.says('a');
b.says('b');
c.says('c');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment