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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -