.ts files run as ES modules under tsconfig module: node16/node18/nodenext, ignoring package.json "type"
Maintainers usually reply within 1 day
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from oxc-project/oxc-node
-
Clarify module resolution with .ts files using import and package.json type commonjsPossibly taken @darcyrush claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
oxc-project/oxc-node#905 ·
Maintainers usually reply within 1 day
-
.ts files whose only imports and exports are type-only run as ES modules, without require or modulePossibly taken @darcyrush claimed this 1 day ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 35/100
oxc-project/oxc-node#909 ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 53/100
oxc-project/oxc-node#906 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
oxc-project/oxc-node#891 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
oxc-project/oxc-node#449 · 1 comment ·
Maintainers usually reply within 1 day
All issues in oxc-project/oxc-node
Similar issues
-
content triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
tensorflow/tfjs#8754 ·
-
tool-submission
Difficulty 1/5 Under an hour Newbie friendliness 83/100
BraveOPotato/NoSignups#2348 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
code-yeongyu/oh-my-openagent#12784 ·
Maintainers usually reply within 1 day
-
external-issue to-triage
Difficulty 2/5 1-3 hours Newbie friendliness 71/100
Maintainers usually reply within 1 day