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


source to share





All Articles