diff --git a/doc/api/esm.md b/doc/api/esm.md index 14a87186aa2e9e..c7675dae16ab62 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -393,6 +393,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)`