I have a Node.js file (code.js), and I'm trying to determine if the file is imported using require or if it is run directly from the command line.
if (/* something here */) {
console.log("This file is run directly using 'node' in the command line.");
} else {
console.log("This file was required from another Node.js file.");
}
If I run node code.js I expect it to print This file is run directly using 'node' in the command line..
However if I create another file that contains require("code.js"); and run that I expect it to print This file was required from another Node.js file..
How can I achieve this?