Repository navigation
feat(core): compile ES module syntax to CommonJS where tsc would (#905) - #911
Conversation
00d8ed3 to
053adbb
Compare
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 053adbb0dc
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
Further to the LLM responses to the comments above, additional output; Limitations of the fix
Out of scope / future changes
|
An ES module compiled to CommonJS resolves its imports through `require()`, so it lost behaviour the ES module resolver gave it: - try tsconfig `paths` and `baseUrl` before Node.js' own resolution, through the new `tsconfigPathCandidates` binding - prefer `config.ts` over `config.json` for an extensionless request - keep a package's compiled `.js` when the resolved file is inside `node_modules` - fall back from `./x.js` to `x.jsx`, and from `./x.jsx` to `x.js`, without letting either win over a file that exists - copy non-enumerable exports in `__importStar`, as tsc does Co-Authored-By: Claude Opus 5.5 <[email protected]>
977e97f to
f4a08d9
Compare
- Reassigned locals, decorated classes and namespaces are `exports.x` itself, as tsc emits them: live, writable, and `undefined` rather than a TDZ error when an import cycle reads them early. Re-exports are getters defined after their `require()`. - An anonymous default function is hoisted as `default_1` and exported up front. - A side-effect import is a bare `require()`. - Only value bindings named like the CommonJS wrapper or the helpers keep a file an ES module; types, ambient declarations and type-only imports do not. - Tests for cycles, a class's statics through a namespace import, `paths` and the `.jsx` fallback in a compiled file. - README: the output's shape, cycles, snapshots for ES importers, the `require` condition, the per-directory `require()` cache, and known limits. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 19c19cd4b3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…ceiver
- `export const a = 1, b = f();` assigns `exports.a` before `f()` runs, as
tsc emits it, so an import cycle sees `a`.
- A reassigned export used as a template tag is called as `(0, exports.tag)`,
as tsc calls it, so it gets no `this`.
- `require(".")` and `require("..")` are relative requests, so a TypeScript
importer finds `index.ts` before a compiled `index.js` next to it, as for
`./` and `../`.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c135b3d23f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…e ES module did - A package `imports` entry or a self-reference through `exports` that names missing compiled output (`#value` -> `./value.js`, only `value.ts` on disk) finds its source through the native resolver, with the `require` condition. - A `.js` file compiled from an ES module resolves an extensionless request TypeScript first, as it did as an ES module. It is told apart from hand-written CommonJS by `exports.__esModule`, which its first statement sets. - A source chosen over compiled output goes back through Node.js' resolution, so a symlinked source gets its real path and loads once. - test: the `--kill-timeout=300` watch test no longer expects the app's and oxnode's lines in one order, which two processes cannot promise. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4406e93e11
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…fallback - A package `imports` entry or self-reference that names missing output now takes the file Node.js mapped it to, which its error names, so every active condition (`--conditions=development`) applies, not a fixed list. - A relative request Node.js cannot find asks the native resolver, which reports its other candidates to watch mode: creating `barrel/index.ts` for `import "./barrel"` restarts the app again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a98adf2579
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
… by the requested name - `export const [a = 1, b = self.a] = v;` becomes `[exports.a = 1, exports.b = self.a] = v;`, as tsc emits it, so each binding is exported before the next default runs. Object patterns and rest elements too; the pattern's own reads go through `exports`. - A package `imports` entry or a self-reference that maps to compiled output next to its source loads the source, as the ES module resolver did. - The source is looked for beside the name a request reaches before Node.js follows a symlink there: `./alias.js` linking to `build/real.js` finds `alias.ts`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f1709b2e4a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…ry a directory's main source - The compiled output's prelude and helpers read `Object` and `Symbol`. A file that declares either at its top level would hand them its own binding before it is initialized, so such a file stays an ES module, as one declaring `require` or a helper's name does. - For `./lib` whose `package.json` `main` names missing output, the source of `main` (`src/main.ts` for `./src/main.js`) is tried before the `index.js` Node.js falls back to, as the ES module resolver tried it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6d218b8eca
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…e ES module order - `./barrel` that Node.js resolves to `barrel/index.json` finds `barrel/index.ts` beside it first, as the ES module resolver did. - An extensionless request takes the ES module resolver's extension order before the aliases of the extension Node.js added, so `./dep` beside `dep.ts`, `dep.tsx` and `dep.jsx` finds `dep.ts`, not `dep.tsx`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cd3b19f4eb
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 771e13fad6
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…o a package, and remap bare requests from lowered JavaScript dependencies - A local file's `./node_modules/pkg` request takes what the ES module resolver finds (`index.mjs`) over Node.js' `index.json`. The resolver already keeps a package's compiled `lib.js` before its `lib.ts`. - A `.js` or `.jsx` dependency oxc-node compiles from an ES module (`OXC_TRANSFORM_ALL`) resolves a bare request in the package order, as a TypeScript one does. A dependency's JavaScript that oxc-node does not compile keeps Node.js' answer. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 52b532f148
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…cies as an ES module A `.js` dependency oxc-node compiles from an ES module (`OXC_TRANSFORM_ALL`) takes the ES module resolver's answer for `./config` too, so `config.ts` comes before `config.json`, and a compiled `lib.js` still before `lib.ts`. A dependency's JavaScript that oxc-node does not compile keeps Node.js' answer. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b80428dfdc
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…s, and remap from lowered extensionless dependencies - The conditions forwarded to the ES module resolver include the `nodeOptions.conditions` (and `addons`) of the configuration file `--experimental-config-file`, `--config-file` or `--experimental-default-config-file` names, which Node.js adds to the command line's. `#dep` takes the `development` branch Node.js took. - An extensionless dependency entry oxc-node compiles from an ES module (`bin/run`, `OXC_TRANSFORM_ALL`) resolves `../config` as an ES module, as a `.js` one does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0a26b97fa4
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Node.js adds a configuration file's `nodeOptions.conditions` to those of the command line, but takes `NODE_OPTIONS`' instead of them. The conditions forwarded to the ES module resolver now do the same, so `#dep` takes the branch Node.js took. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 24ae4ebd56
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
… a configuration file with a byte order mark - A request the ES module resolver cannot read (`./dep#x.js`) no longer takes the source beside a symlink's target: `dep#x.js` linking to `build/real.js` keeps it, as Node.js found it, when no `dep#x.ts` stands beside the requested name. - The configuration file whose conditions are forwarded may start with a byte order mark, as Node.js accepts it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 60b755a77f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…onditions, and keep an exact extensionless link - The `exports.a = exports.b = … = void 0` prelude is split into chains of 50, as `tsc` emits it: one chain of thousands of exports nested too deep for V8 to compile. - The ES module resolver maps a request with the conditions Node.js hands `_resolveFilename` as `options.conditions`, which a `module.registerHooks()` hook may have changed, and with the startup ones otherwise. - An extensionless request whose exact name is a symlink (`./bin#x`) keeps the file it links to, as Node.js takes the exact file first, when no source stands beside that name. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 07fb88136d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…does, and `--no_addons` Checked against Node.js 22.23, 24.15, 24.21, 26.0 and 26.13: - `NODE_OPTIONS`, set at all, even empty, keeps Node.js from taking a configuration file's options, so its conditions are not forwarded then. - Given `--experimental-config-file` and `--experimental-default-config-file` both, Node.js 24.19 and 26.1 and later take the flag that comes last, earlier versions the named file; the forwarded conditions follow. - `--no_addons` is `--no-addons`, as Node.js reads `_` in an option's name. - A configuration file's `addons` is no longer read: Node.js 22 ignores it, and where Node.js takes it, the native binding cannot load. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 303f39ebd0
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…ddons` condition `node-addons` is forwarded unless `process.dlopen()` reports `ERR_DLOPEN_DISABLED`, which Node.js throws before it looks for the file whenever addons are off: by `--no-addons`, `--no_addons`, or a configuration file's `addons` where the running version reads it (Node.js 22 does not, 24 and 26 do, and not with `NODE_OPTIONS` set). This replaces parsing those flags. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cc5e84ae1b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…file sets NODE_OPTIONS Node.js drops a configuration file's options when the process starts with `NODE_OPTIONS`, not when an `--env-file` sets it: it reads that file later, and keeps a variable the environment already has. `NODE_OPTIONS` that holds what an `--env-file` or `--env-file-if-exists` of the command line sets counts as unset for this, checked on Node.js 22.23, 24.15, 24.21 and 26.13. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7b0986e43a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
#908 and #910 are merged; this branch is up to date with
main.AI generated, but ran the fix against a full live CJS project. The project uses
package.jsontype ascommonjswith all file extensions as.tsrunning NestJS 12 (ESM) and tests mocked using nodes test modulemock.method.I needed to upgrade to TypeScript 7.0.2 from 5.9.3 which ts-node doesn't support. tsx and other don't support legacy TypeScript decorators (or rather
emitDecoratorMetadata, which NestJS uses). I'm not aware of any other JIT that supports CJS projects needing emit decorator metadata on TS7+.Closes #905
Problem
In a
"type": "commonjs"package, a.tsfile that usesimport/exportruns as an ES module under oxc-node, even when tsconfig compiles it to CommonJS. Its namespace object is then frozen, somock.method()fromnode:testcan't replace its exports. Undertscit can.Node.js' own type stripping doesn't run this file as an ES module either: it rejects it with
SyntaxError: Cannot use import statement outside a module.Fix
A file with ES module syntax is compiled to CommonJS the way
tsccompiles it when all of these hold:"type": "commonjs"moduleiscommonjs,node16,node18,node20ornodenext(any case).ts,.tsx,.js,.jsxor noneimport.metanor top-level await, which have no CommonJS formexports,require,module,__filename,__dirname,__importDefault,__importStar,__exportStarAnywhere else, such as a package with no
"type","type": "module", or an ESmodulesetting, the file runs as an ES module, as before. Switching a project's"type"to"module"turns the compilation off.The output has
tsc's shape. It's built from the module runner transform's output:exports.a = void 0; … exports.a = a;). It's writable, somock.method(namespace, "a")reaches every importer. A hoisted function is exported up front.exports.aitself, astscemits them: live, writable, andundefined(not a TDZ error) when an import cycle reads them early. Re-exports stay live through getters, in the formcjs-module-lexerdetects, defined after theirrequire(). A reassigned local exported under two names, declared by destructuring or set to an anonymous function also gets a getter.default_1and exported up front. A side-effect import is a barerequire().require()'s result. Default and namespace imports go through__importDefault/__importStar,export *through__exportStar, andimport()staysimport().format: "commonjs", so an ES module importer gets the named exports.The compiled code
require()s./lib.jswhere the source imported./lib.js. SoModule._resolveFilenameinregister.mjsfalls back to the TypeScript source (.js→.ts/.tsx,.jsx→.tsx/.ts,.mjs→.mts,.cjs→.cts). When the output is missing,./x.jsalso findsx.jsxand./x.jsxfindsx.js. For a TypeScript importer outsidenode_modules, it also prefers the source over compiled output sitting beside it, as the ES module resolver does since #840, andconfig.tsoverconfig.jsonfor./config; a file insidenode_moduleskeeps its compiled.js. A bare request tries tsconfigpathsandbaseUrlfirst, through the newtsconfigPathCandidatesbinding. The hook thread's helper tag is registered on the main thread too, so the compiled files canrequire()their helpers.Under
node16,node18andnodenext, the resolve hook no longer reports every.tsfile as an ES module; the package's"type"decides, as it does fortsc. #908 made that change and is merged.Behaviour changes
These apply only to projects with
"type": "commonjs"and one of themodulesettings above:import/exportnow runs as CommonJS. An ES module's default import of it ismodule.exports, with the file's default export at.default, the same as TypeScript types it undernodenext.export *from another TypeScript file: Node.js only follows re-exports into.jsand.cjsfiles.node16/node18/nodenext, a.tsfile without module syntax is CommonJS, also when an ES module imports it.requirecondition, as undertsc; a package whoseexportsoffers onlyimportfails.require()per directory and request, so when a.jsfile and a.tsfile in one directory bothrequire("./lib.js")next tolib.ts, whichever asks first decides which file both get.require()of an ES module from a compiled file loaded through the module hooks needs esm: populate separate cache for require(esm) in imported CJS nodejs/node#59679 (22.22.3 on the 22.x line). Earlier versions fail withTypeError: Cannot read properties of undefined (reading 'exports').The README has a new "ES module syntax in a CommonJS package" section covering this.
Tests
commonjs_loweringcovers eachmodulesetting, the output shape (reassigned exports, side-effect imports, the anonymous default function), the cases that stay ES modules (import.meta, reserved names,esnext,preserve,.mts, a package without"type"), and type-only ordeclared reserved names that still compile.packages/integrate-commonjspackage ("type": "commonjs",module: nodenext, run withoxnode --test), 20 tests:.jsnames finding.ts, with the source preferred over compiled output beside itundefinedfor a reassignedlet, a decorated class, a namespace and a re-export read earlypathsalias and the.js→.jsxfallbackimport.metaand top-level await staying ES modulescommonjs.test.tsfails to load onmain.helpers.test.tspasses on both and guards what must not change.commonjs-lowering.spec.ts(vitest) coverspathsandbaseUrl,config.tsbesideconfig.json, the.js/.jsxfallbacks,node_modulesfiles and non-enumerable exports.vp run test(643 vitest tests plus the threenode --testpackages),cargo test, clippy and fmt pass.