Skip to content

Instantly share code, notes, and snippets.

@chetbox
Created July 23, 2015 10:20
Show Gist options
  • Save chetbox/5dd0801d935a843bd3b0 to your computer and use it in GitHub Desktop.
Save chetbox/5dd0801d935a843bd3b0 to your computer and use it in GitHub Desktop.
A node module whose exports can optionally be made global
/*
This module can be required normally:
var optionally_global = require('./optionally_global');
console.log(optionally_global.bar());
Or its exports can be made global:
require('./optionally_global').global();
console.log(bar());
*/
exports.foo = 'foo';
exports.bar = function() {
return 'bar';
}
exports.global = function() {
for (var cmd in exports) {
if (cmd !== 'global') {
if (!global[cmd]) {
global[cmd] = exports[cmd];
} else {
console.warn('Global function "' + cmd + '" already defined');
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment