microsoft / microsoft/TypeScript

Add a module resolution mode to get module search path from Node.js executable itself

Offen
#41,981 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

Search Terms

node path
module path
NODE_PATH

Suggestion

TypeScript's effort to match Node.js's module resolution doesn't seem to always be accurate. Further, it can never be right if Node.js has been modified or configured in any way that does not match the emulation. It should be possible for TypeScript to locate type definitions using the actual module search path that node will be using at runtime, so that the runtime and the static type checking agree exactly on which files are used. After all, the TypeScript compilation is meant to reflect static properties of the code!

Use Cases

My organization has a monorepo-style integration build of first party modules which are available in $PREFIX/lib/node. This path is used by the node executable when processing require, but TypeScript sadly needs to be taught it through tsconfig baseUrl or similar hack. I believe this should be zero config and would fix a bunch of other similar use cases where the Node.js runtime disagrees with the TypeScript compiler.

Examples

One suggested way to set this up would be a new moduleResolution mode, alongside node and classic, which rather than emulating Node.js, actually calls Node.js itself to evaluate the actual module search path which will be used by a project. This mode could be called e.g. nodeIntrospect or something bikesheddable.

A facility to specify the command used to get the module search path could be provided for advanced usages such as running Node.js in a Docker container or other isolation mechanism. The command would merely be required to return a JSON array of paths or something.

(At this point I'm unsure if Node.js actually provides something similar to Python's sys.path for this, but if this issue is accepted in principle I could try to expose something for this in Node.js.)

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Much appreciated! Thanks!

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 werden keine Quelldateien oder Tests genannt. Beginne damit, TypeScripts bestehende Handhabung von moduleResolution zu lokalisieren und zu recherchieren, wie Node.js den Laufzeit-Modulsuchpfad bereitstellt. Als abgeschlossen gilt eine vereinbarte Konzeption und Implementierung, die Typdefinitionen anhand der tatsächlichen Node.js-Suchpfade auflöst, einschließlich der Abdeckung der vorgeschlagenen Konfigurations- und Befehlsvarianten.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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