-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Provide an equivalent of require.main for ESM #57616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
cc @marco-ippolito @GeoffreyBooth I think we should add this to the "ESM parity" initiative. |
For reference #49440 |
I don't think we should wait for wintercg, but rather add a getter in process or something in util. |
Since it's per-module, it'd have to either live on |
#49440 has pretty much all the context. I think someone could read that full thread and just go ahead and build it at this point; there are some points around how to treat worker entry points that might still need working out, but those finer details could happen within the PR. (@mcollina you know a lot about worker threads, if you want to tackle this 😉.) Ideally our implementation would be compatible/interoperable with Deno’s. See https://github.com/wintercg/import-meta-registry#:~:text=webappapis.html%23hostgetimportmetaproperties-,import.meta.main,-Returns%20whether%20the and https://docs.deno.com/runtime/reference/deno_namespace_apis/#importmeta--api:~:text=com/main.ts-,import.meta.main,-Returns%20whether%20the |
Another duplicate #57226 |
Closing then. |
Currently there is no equivalent of
require.main
for ESM scripts.Note that this is not listed at all in https://nodejs.org/api/esm.html#differences-between-es-modules-and-commonjs either.
The "known" equivalent would be to use
process.argv[1] === new URL(import.meta.url).pathname)
, but it doesn't work with symbolic links (e.g. npm bin scripts).A solution is to use https://www.npmjs.com/package/es-main, which practically goes down to
require
to implement it. I think we should really include something out of the box.The text was updated successfully, but these errors were encountered: