Nodejs how to determine if a module is enabled

How can I determine if server.js is enabled or works directly, so I can export the server or just server.listen (port)?

thanks

+7
source share
1 answer

From http://nodejs.org/docs/latest/api/modules.html#accessing_the_main_module

When a file is launched directly from Node, require.main is installed in its module. This means that you can determine if the file was run directly by testing

require.main === module

+9
source

All Articles