diff --git a/doc/api/esm.md b/doc/api/esm.md index b3b061b2bb05be..551c71d446dd85 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -396,6 +396,35 @@ import { readFileSync } from 'node:fs'; const buffer = readFileSync(new URL('./data.proto', import.meta.url)); ``` +### `import.meta.main` + + + +> Stability: 1.0 - Early development + +* {boolean} `true` when the current module is the entry point of the current process; `false` otherwise. + +Equivalent to `require.main === module` in CommonJS. + +Analogous to Python's `__name__ == "__main__"`. + +```js +export function foo() { + return 'Hello, world'; +} + +function main() { + const message = foo(); + console.log(message); +} + +if (import.meta.main) main(); +// `foo` can be imported from another module without possible side-effects from `main` +``` + ### `import.meta.resolve(specifier)`