nornagon nornagon - 9 months ago 40
Node.js Question

node.js equivalent of python's if __name__ == '__main__'

I'd like to check if my module is being included or run directly. How can I do this in node.js?


The docs describe another way to do this which may be the preferred method:

When a file is run directly from Node, require.main is set to its module.

To take advantage of this, check if this module is the main module and, if so, call your main code:

var fnName = function(){
    // main code

if (require.main === module) {