Skip to content

fix: resolve CommonJS directory requires inside dependencies#803

Merged
privatenumber merged 2 commits into
privatenumber:masterfrom
imevanc:fix/node-modules-extension-rewrite
May 31, 2026
Merged

fix: resolve CommonJS directory requires inside dependencies#803
privatenumber merged 2 commits into
privatenumber:masterfrom
imevanc:fix/node-modules-extension-rewrite

Conversation

@imevanc

@imevanc imevanc commented May 23, 2026

Copy link
Copy Markdown
Contributor

Problem

On Node 24+, a CommonJS require() inside a dependency flows through tsx's sync ESM hook, where directory-style specifiers (require('..'), require('process/')) were resolved to a file:// URL. Node's CommonJS resolver rejects file:// URLs in a require() context, so every candidate failed — including the real .js/.ts — and the last-tried .jsx surfaced:

Error: Cannot find module '.../node_modules/ajv/dist/compile/index.jsx'

Changes

In a CommonJS require() context, resolveDirectorySync:

  • defers bare specifiers with a trailing slash (e.g. require('process/')) to Node's package resolution;
  • resolves relative directories (require('..'), require('./dir/')) from a filesystem path, falling back to the directory's package.json main when no index file exists.

The ESM import path is unchanged.

Closes #800

torbjornvatn added a commit to torbjornvatn/tsx-json-bug that referenced this pull request May 28, 2026
Replace statuses/JSON repro with superagent-based HTTP fetch so the
test exercises tsx's CJS require() hook — the actual failure surface
on Node 24 where tsx wrongly rewrites intra-node_modules relative
paths (.js → .jsx/.ts etc.).

- swap statuses → superagent (CJS pkg with internal require() calls)
- src/status.ts: fetchJson() wraps superagent GET
- src/status.test.ts: offline local http server, two JSON round-trips
- add tsx-pr (file:../tsx-fix) — local build of PR #803 fix branch
- add test:pr script; drop @types/statuses
- update README to document new bug, fix, and all four test variants

Refs privatenumber/tsx#800, privatenumber/tsx#803
@privatenumber

Copy link
Copy Markdown
Owner

Thanks for kicking this off, @imevanc! I pushed a commit that extends it to fix the underlying cause more broadly.

The directory resolution was handing Node's CommonJS resolver a file:// URL, which it rejects in a require() context — so even the real .js/.ts file failed and the last-tried .jsx is what surfaced. It now resolves the implicit index from a filesystem path.

That also covers two related shapes from the same code path:

  • Bare package specifiers with a trailing slash. readable-stream does require('process/') to load its process dependency — the trailing slash deliberately selects the npm package over Node's builtin, so it's a bare specifier, not a relative path. tsx was resolving it against the parent dir (.../streams/process/index.jsx); it's now left to Node's normal package resolution.
  • Directory requires that resolve through a nested package.json main.

Added regression tests for each, and CI is green across the Node matrix.

@privatenumber privatenumber changed the title fix: resolve node-modules extension rewrite fix: resolve CommonJS directory requires inside dependencies May 31, 2026
@privatenumber privatenumber merged commit 1ce8463 into privatenumber:master May 31, 2026
2 checks passed
@privatenumber

Copy link
Copy Markdown
Owner

This issue is now resolved in v4.22.4.

If you're able to, your sponsorship would be very much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Node 24: tsx incorrectly resolves .js files in node_modules to .jsx/.tsx extensions

2 participants