if __name__ == '__main__' js website 1

if __name__ == ‘__main__’ js website

var fnName = function() {
    // main code
}

if (require.main === module) {
    fnName();
}

Here is what the above code is Doing:
1. If the module is being run directly (as opposed to being imported), then require.main will be set to its module. That means that require.main.filename will contain the path that was used to execute the code.
2. If the module is being imported, then require.main will not be set to its module. That means that require.main.filename will not be defined.
3. If the module is being run directly, then run the code. Otherwise, the module is being imported for some other purpose, so don’t run the code.

Similar Posts