Can I know, in node.js, if my script is being run directly or being loaded by another script?

You can use module.parent to determine if the current script is loaded by another script.

e.g.

a.js:

if (!module.parent) {
    console.log("I'm parent");
} else {
    console.log("I'm child");
}

b.js:

require('./a')

run node a.js will output:

I'm parent

run node b.js will output:

I'm child

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)