Skip to content

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

Closed
mcollina opened this issue Mar 25, 2025 · 7 comments
Closed

Provide an equivalent of require.main for ESM #57616

mcollina opened this issue Mar 25, 2025 · 7 comments
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. feature request Issues that request new features to be added to Node.js.

Comments

@mcollina
Copy link
Member

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.

@mcollina
Copy link
Member Author

cc @marco-ippolito @GeoffreyBooth I think we should add this to the "ESM parity" initiative.

@mcollina mcollina added the feature request Issues that request new features to be added to Node.js. label Mar 25, 2025
@github-project-automation github-project-automation bot moved this to Awaiting Triage in Node.js feature requests Mar 25, 2025
@marco-ippolito
Copy link
Member

marco-ippolito commented Mar 25, 2025

For reference #49440

WinterTC55/import-meta-registry#4

@mcollina
Copy link
Member Author

I don't think we should wait for wintercg, but rather add a getter in process or something in util.

@ljharb
Copy link
Member

ljharb commented Mar 25, 2025

Since it's per-module, it'd have to either live on import.meta or take import.meta.url as an argument.

@GeoffreyBooth
Copy link
Member

#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

@GeoffreyBooth GeoffreyBooth added the esm Issues and PRs related to the ECMAScript Modules implementation. label Mar 25, 2025
@joyeecheung
Copy link
Member

Another duplicate #57226

@mcollina
Copy link
Member Author

Closing then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. feature request Issues that request new features to be added to Node.js.
Projects
Archived in project
Development

No branches or pull requests

5 participants