nodejs / nodejs/node

Provide a reliable way to find the entrypoint path or URL

Open
#51,840 21 comments 17 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request loaders
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No implementation file or test is named. Start by reviewing how Node.js currently determines the CLI entrypoint for CommonJS and ESM, including the require.main.filename and process.argv[1] alternatives described here. Done means a stable API works for both module systems and handles extensionless paths, symlinks, and later process.argv changes.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.