Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created April 9, 2014 08:49
Show Gist options
  • Save Raynos/10243323 to your computer and use it in GitHub Desktop.
Save Raynos/10243323 to your computer and use it in GitHub Desktop.
/* usage:
var requireMain = require('require-main')
// instead of `require.main === module`
if (requireMain(require, module)) {
doStuff(process.argv)
}
*/
var MAGIC_BROWSERIFY_LINES = 8;
function nodeMain(require, module) {
return require.main === module;
}
function browserMain() {
var error;
try {
throw new Error('browserMain')
} catch (e) {
error = e;
}
var lines = error.stack && error.stack.split('\n')
return lines.length < MAGIC_BROWSERIFY_LINES;
}
if ("main" in require) {
module.exports = nodeMain;
} else {
module.exports = browserMain;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment