Skip to content
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

Provide a reliable way to find the entrypoint path or URL #51840

Open
nex3 opened this issue Feb 22, 2024 · 3 comments
Open

Provide a reliable way to find the entrypoint path or URL #51840

nex3 opened this issue Feb 22, 2024 · 3 comments
Labels
feature request Issues that request new features to be added to Node.js. loaders Issues and PRs related to ES module loaders

Comments

@nex3
Copy link

nex3 commented Feb 22, 2024

What is the problem this feature will solve?

This solves the same problem as require.main.filename did in a CommonJS-only world: finding the entrypoint script for the Node.js process. This is most commonly used to determine the whether the current file is the entrypoint, but is also useful for various other sorts of introspection about the running Node.js environment. My specific use-case is finding a sensible default base URL to use to replicate Node.js's package resolution algorithm.

What is the feature you are proposing to solve the problem?

I suggest something like process.cliEntrypoint or process.cliMainUrl. I think it's important that any solution be usable regardless of whether the current module is CJS or ESM.

What alternatives have you considered?

require.main.filename provides this information but it doesn't work outside of CJS files, nor does it work if the entrypoint itself is ESM.

process.argv[1] sometimes provides this information, but it has a number of problems:

  • If the Node.js CLI is run without a file extension, like node myapp instead of node myapp.js, process.argv[1] won't have an extension either.

  • If the entrypoint is a symlink, process.argv[1] will point to the symlink instead of the physical JS file. This differs from __filename, which means it also differs from the logic used by require(). Note that npm exec always creates a symlink when running executables.

  • process.argv is mutable, so it's possible for unrelated code to modify it in such a way that it's no longer usable to find the entrypoint.

@nex3 nex3 added the feature request Issues that request new features to be added to Node.js. label Feb 22, 2024
Copy link
Contributor

There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-stale Mark issue so that it is never considered stale label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot added the stale label Aug 21, 2024
@nex3
Copy link
Author

nex3 commented Aug 21, 2024

Please don't close this! It would still be very useful!

@github-actions github-actions bot removed the stale label Aug 22, 2024
@RedYetiDev RedYetiDev added the loaders Issues and PRs related to ES module loaders label Aug 22, 2024
@RedYetiDev
Copy link
Member

@nodejs/loaders

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. loaders Issues and PRs related to ES module loaders
Projects
Development

No branches or pull requests

2 participants