Skip to content

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

Closed

Description

@darcyrush

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.

Activity

  1. added a commit that references this issue on Nov 9, 2026
    5a6aac8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions