nodejs / nodejs/node

Provide a reliable way to find the entrypoint path or URL

Offen
#51,840 21 Kommentare 17 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature request loaders
Vorherrschende Sprache
JavaScript
Sterne
122k
Forks
37.3k
Ø Merge
4 T. 2 Std.
Gemergte PRs (30 T.)
283

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Es ist keine Implementierungsdatei und kein Test angegeben. Beginne damit zu prüfen, wie Node.js derzeit den CLI-Einstiegspunkt für CommonJS und ESM bestimmt, einschließlich der hier beschriebenen Alternativen require.main.filename und process.argv[1]. Erledigt bedeutet, dass eine stabile API für beide Modulsysteme funktioniert und pfadlose Erweiterungen, symbolische Links sowie spätere Änderungen an process.argv verarbeitet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, node.js
Bereich
backend
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.