Teodor Pripoae
Teodor Pripoae

Reputation: 1394

Nodejs how to detect if module is included

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

Answers (1)

thejh
thejh

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

Related Questions