microsoft / microsoft/TypeScript

CommonJS globals permitted for ES module builds with no compiler error.

Offen
#58,658 9 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 Search Terms

nodenext, module, __dirname

🕗 Version & Regression Information

Version 5.4.5

⏯ Playground Link

No response

💻 Code

package.json

"type": "module"

tsconfig.json

{
  "compilerOptions": {
    "module": "NodeNext"
  },
  "include": ["src"]
}

file.ts

console.log(__dirname)
🙁 Actual behavior

No compiler error, but this causes a runtime error in Node:

ReferenceError: __dirname is not defined in ES module scope
🙂 Expected behavior

That the compiler issues an error similar to the inverse situation.

For example, when targeting CommonJS:

package.json

"type": "commonjs"

file.ts

console.log(import.meta.dirname)

The compiler issues the following error:

error TS1470: The 'import.meta' meta-property is not allowed in files which will build into CommonJS output.
Additional information about the issue

Perhaps there is a good reason for this that I'm not understanding, but I would think tsc should issue an error or warning for any syntax that would produce a runtime error.

Here is a more complete example: https://github.com/knightedcodemonkey/tsc-module-globals

  • npm install
  • npm run esm (note no compile error but the output causes a runtime error)
  • npm run cjs (note there is a compile error)

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

Reproduziere das Problem mit package.json, tsconfig.json und file.ts unter Verwendung der referenzierten npm run esm- und npm run cjs-Befehle. Vergleiche die vorhandene CommonJS-import.meta-Diagnose mit dem __dirname-Fall im ES-Modul; abgeschlossen bedeutet, dass der NodeNext-ES-Modul-Build das ungültige CommonJS-Global vor der Laufzeit meldet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
node.js, typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

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