Node.JS: Detect if called through require or directly by command line -
how can detect whether node.js file called using sh:node path-to-file
or js:require('path-to-file')
?
this node.js equivalent previous question in perl: how can run perl script if wasn't loaded require?
if (require.main === module) { console.log('called directly'); } else { console.log('required module'); }
see documentation here: https://nodejs.org/docs/latest/api/all.html#modules_accessing_the_main_module
Comments
Post a Comment