Reputation: 1394
How can I detect if server.js is included or directly ran, so I can export.server or just server.listen(port) ?
Thanks
Upvotes: 8
Views: 614
Reputation: 45568
From http://nodejs.org/docs/latest/api/modules.html#accessing_the_main_module
When a file is run directly from Node, require.main is set to its module. That means that you can determine whether a file has been run directly by testing
require.main === module
Upvotes: 10