On this page

    Accessing the main module

    History

    When a file is run directly from Node.js, require.main is set to its module. That means that it is possible to determine whether a file has been run directly by testing require.main === module.

    For a file foo.js, this will be true if run via node foo.js, but false if run by require('./foo').

    When the entry point is not a CommonJS module, require.main is undefined, and the main module is out of reach.