How do I know if a node script is running directly or included?

I have a node.js file:

#!/usr/bin/env node

module.exports = 'foo';

      

I am trying to add at the bottom:

if(is run as shell script){
    console.log(module.exports);
}

      

Is there a correct way to do this? I can hack something using things like process.argv

and __filename

, but is this the best way?

+3
node.js


source to share


No one has answered this question yet

See similar questions:

253
node.js python equivalent if __name__ == '__main__'

or similar:

868
In Node.js, how do I include features from other files?
867
How to remove npm modules in js node?
861
How do I get the path to the current script using Node.js?
499
Read a file one line at a time in node.js?
340
How to run a shell script on startup
274
How to run hello.js file in Node.js on windows?
61
Change working directory in my current shell context when running Node script
fourteen
Starting a node from a bash script
4
How can I run an ES6 module as a script in a node?
0
Include existing js script in node.js



All Articles
Loading...
X
Show
Funny
Dev
Pics