Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs how to detect if module is included

Tags:

node.js

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

Thanks

like image 454
Teodor Pripoae Avatar asked Oct 31 '11 16:10

Teodor Pripoae


People also ask

How do you check if a module is installed in Node JS?

To check for all locally installed packages and their dependencies, navigate to the project folder in your terminal and run the npm list command. You can also check if a specific package is installed locally or not using the npm list command followed by package name.

Is used to include module in node JS?

Which function is used to include modules in Node Js. require(); 8.

How do I get a list of node modules?

To list the modules installed locally in a project, enter the project directory and execute the npm list command, as shown in the example below.


1 Answers

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

like image 122
thejh Avatar answered Oct 04 '22 16:10

thejh