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

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -

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

javascript - Restarting Supervisor and effect on FlaskSocketIO -