Zet - What is the equivilent to if __main__ in python for node js?
What is the equivilent to if __main__ in python for node js?
Use this syntax. See Modules: CommonJS modules | Node.js v22.3.0 Documentation
function myMain() {
// main code
}
if (require.main === module) {
myMain();
}
EDIT: If you use this code in a browser, you will get a “Reference error” since “require” is not defined. To prevent this, use:
if (typeof require !== 'undefined' && require.main === module) {
myMain();
}
#nodejs