if (!module.parent) {
// this is the main module
} else {
// we were require()d from somewhere else
}
EDIT: If you use this code in a browser, you will get a "Reference error" since "module" is not defined. To prevent this, use:
if (typeof module !== 'undefined' && !module.parent) {
// this is the main module
} else {
// we were require()d from somewhere else or from a browser
}