.ts files run as ES modules under tsconfig module: node16/node18/nodenext, ignoring package.json "type"

Aperta
#907 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@darcyrush ci sta già lavorando.

Dal 6/11/2026.

  • #908 di @darcyrush — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
node.js, typescript
Ambito
backend

Direzione di ricerca

Start with default_module_from_tsconfig in src/lib.rs and trace how node16, node18, and nodenext determine the module format for .ts and .tsx files. Reproduce with the provided require example under --import @oxc-node/core/register, then check the behavior for imported CommonJS files and test files run with --test. Done means these settings respect the package "type" and the reproduction behaves as expected; pull request #908 is already open.

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

Descrizione

While investigating #905, the LLM stated it found a bug;

Versions: oxc-project/oxc-node main (ffa98c1), @oxc-node/core 1.0.1, Node.js v26.12.1, linux-x64

Repro

package.json (or no "type")

{ "type": "commonjs" }

tsconfig.json (same with node16 or node18)

{ "compilerOptions": { "module": "nodenext" } }

entry.ts

const os = require("node:os");
console.log("ok", typeof os.platform);
node --import @oxc-node/core/register ./entry.ts

Expected: ok function. Under node16, node18 and nodenext, tsc emits each .ts file in the format its package.json "type" gives it, and a package without "type" is CommonJS. So tsc compiles this file to CommonJS. Node's own type stripping (node ./entry.ts) prints the same.

Actual: ReferenceError: require is not defined in ES module scope, you can use import instead

TypeScript's own CommonJS syntax fails earlier, when the file is transformed:

import os = require("node:os");
console.log("ok", typeof os.platform);
Error: Failed to transform …/entry.ts:
  ! TS(1202): Import assignment cannot be used when targeting ECMAScript modules.

tsc compiles this to const os = require("node:os");.

The same happens to a CommonJS .ts file imported from an ES module, and to test files run with --test. module: node20 is not affected, because there the package type already decides.

Cause

default_module_from_tsconfig (src/lib.rs) groups node16, node18 and nodenext with the ES module settings (es6, es2015 … esnext). That makes the resolve hook report every .ts/.tsx file as an ES module under those settings.

Lingua principale
TypeScript
Stelle
275
Fork
10
Merge medio
5h 31m
PR unite (30g)
68

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

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.

Altre issue di oxc-project/oxc-node

Tutte le issue di oxc-project/oxc-node

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.