@@ -391,6 +391,29 @@ import { readFileSync } from 'node:fs';
391
391
const buffer = readFileSync (new URL (' ./data.proto' , import .meta.url));
392
392
` ` `
393
393
394
+ ### ` import .meta.main`
395
+
396
+ <!-- YAML
397
+ added:
398
+ - REPLACEME
399
+ -->
400
+
401
+ > Stability: 1.2 - Release candidate <!-- TODO: is this correct? -->
402
+
403
+ * {boolean} ` true` when the current module is the entry point of the current process; ` false` otherwise.
404
+ * Equivalent to ` require.main === module` in CommonJS. <!-- TODO: Is this still accurate? -->
405
+ * Analogous to Python's ` __name__ == " __main__" ` .
406
+
407
+ [` import.meta.resolve` ][]
408
+
409
+ ` ` ` js
410
+ function main () {
411
+ console .log (' Hello, world!' );
412
+ }
413
+
414
+ if (import .meta.main) main();
415
+ ` ` `
416
+
394
417
### ` import .meta.resolve(specifier)`
395
418
396
419
<!-- YAML
@@ -607,6 +630,10 @@ These CommonJS variables are not available in ES modules.
607
630
They can instead be loaded with [` module.createRequire()` ][] or
608
631
[` process.dlopen` ][].
609
632
633
+ #### No ` require.main`
634
+
635
+ To replace ` require.main === module` , there is the [` import.meta.main` ][] API.
636
+
610
637
#### No ` require.resolve`
611
638
612
639
Relative resolution can be handled via ` new URL(' ./local' , import.meta.url)` .
@@ -1167,6 +1194,7 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][].
1167
1194
[` import()` ]: #import-expressions
1168
1195
[` import.meta.dirname` ]: #importmetadirname
1169
1196
[` import.meta.filename` ]: #importmetafilename
1197
+ [` import.meta.main` ]: #importmetamain
1170
1198
[` import.meta.resolve` ]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve
1171
1199
[` import.meta.url` ]: #importmetaurl
1172
1200
[` import` ]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
0 commit comments