microsoft / microsoft/TypeScript

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

Aperta
#41,981 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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!

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Non vengono indicati file sorgente o test. Inizia individuando la gestione esistente di moduleResolution in TypeScript e ricercando come Node.js espone il percorso di ricerca dei moduli a runtime. Il lavoro sarà considerato completato quando saranno concordate una progettazione e un’implementazione che risolvano le definizioni dei tipi dai percorsi di ricerca effettivi di Node.js, con una copertura delle varianti proposte di configurazione e comando.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
node.js, typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.