Nodejs如何检测是否包含模块

时间:2022-10-12 23:22:13

How can I detect if server.js is included or directly ran, so I can export.server or just server.listen(port) ?

如何检测server.js是否包含或直接运行,所以我可以export.server或只是server.listen(端口)?

Thanks

1 个解决方案

#1


9  

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

直接从Node运行文件时,require.main设置为其模块。这意味着您可以通过测试确定文件是否已直接运行

require.main === module

require.main === module

#1


9  

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

直接从Node运行文件时,require.main设置为其模块。这意味着您可以通过测试确定文件是否已直接运行

require.main === module

require.main === module