Undefined Title

How to know whether it's loaded as module or top-level on Node.js


As you know, there is a famous idiom on Python, which is if __main__ == "__main__":.

The inside of if is only executed when it is directly launched by python command. It’s the way that we make command line interface which is compatible to module.

On Node.js, we can do same thing.

You can see next if you run them.

$ node app.js
started: app.js

$ node cli.js
started: cli.js

module.parent is the way. If it’s directly launched, it becomes null. Very simple.