Using! Module.parent in nodejs

Could you explain why we use module.parent module in node. why node.js access to parent modules

if (!module.parent) { app.listen(3000); console.log('listening on port 3000'); } 
+7
source share
2 answers

I have found the answer. You can use module.parent to determine if the current script is loaded by another script. Example:

 a.js: if (!module.parent) { console.log("I'm parent"); } else { console.log("I'm child"); } b.js: require('./a') run node a.js will output: I'm parent run node.b.js will output: I'm child 
+11
source share

In hierarchical programming paradigms, many tasks are performed at a higher level in the hierarchy of the structure, which provides better performance and efficiency. The same applies here. If the parent of the running module is not listening on any port, then this task is performed by its child explicitly.

+4
source share

All Articles