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

Open
#907 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@darcyrush is already working on this.

Since Nov 6, 2026.

  • #908 by @darcyrush — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
node.js, typescript
Domain
backend

Research direction

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.

Written by the indexing model from the issue text.

Description

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.

Dominant language
TypeScript
Stars
275
Forks
10
Avg merge
5h 31m
Merged PRs (30d)
68

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from oxc-project/oxc-node

All issues in oxc-project/oxc-node

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.