Behavior
-
CommonJS Detection:
1if (typeof mainModule !== "undefined" && cjsModule === mainModule) {2 return require.main === mainModule;3}Checks if the current
moduleis the main script. -
ES Module Detection:
1const filename = fileURLToPath(importMetaUrl);2return filename === process.argv[1];Converts
import.meta.urlto a path and compares with the Node.js entry file. -
Safe Fallback: Returns
falsefor unknown or invalid inputs.