Skip to content

fix: restore the CJS bridge namespace for Node 24 require(esm) under tsImport() - #802

Merged
privatenumber merged 5 commits into
privatenumber:masterfrom
ryanbonial:fix/cjs-bridge-artifact-esm-resolve
Sep 20, 2026
Merged

privatenumber merged 5 commits into
privatenumber:masterfrom
ryanbonial:fix/cjs-bridge-artifact-esm-resolve

Conversation

@ryanbonial

@ryanbonial ryanbonial commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On Node 24, tsImport() throws ERR_MODULE_NOT_FOUND when the imported file is a CommonJS-package .ts that re-exports an ES module. The same code loads on Node 22, so upgrading to Node 24 breaks programs that load their config through tsImport().

In a CommonJS package:

// config.ts
export { value } from './dep.mjs';
// run.mjs
import { tsImport } from 'tsx/esm/api';
await tsImport(new URL('./config.ts', import.meta.url).toString(), import.meta.url);

Node 24 reports Cannot find module '.../dep.mjs?namespace=<id>' imported from .../config.ts?namespace=<id> (#801).

Changes

tsx's CommonJS bridge appends ?namespace=<id> to each resolved filename for per-tsImport() require cache isolation. On Node 24, require(esm) hands that filename to the ESM loader, and pathToFileURL() represents the ? in the URL as %3F. tsx's resolve hook only read a namespace from a real query string, so Node looked for the filename dep.mjs?namespace=<id> (represented in the URL as dep.mjs%3Fnamespace=<id>) and missed.

The resolve hook now recognizes that bridge artifact and restores it as the namespace query, so namespaced resolution and isolation apply as before. It reverses only the %25 layer that pathToFileURL() added, then decodes the recovered query values; it rewrites the pathname only when the trailing encoded query's namespace matches the active tsImport() namespace. Literal percent-encoded question-mark paths (file%3Fname.ts) and tsx's marked tsx-commonjs-virtual-query=1 URLs stay untouched.

Closes #801

@privatenumber privatenumber changed the title fix: strip CJS-bridge artifact in ESM resolve to unblock Node 24 require(esm) under tsImport() fix: restore the CJS bridge namespace for Node 24 require(esm) under tsImport() Sep 20, 2026
@privatenumber
privatenumber merged commit 6e5236b into privatenumber:master Sep 20, 2026
2 checks passed
@privatenumber

Copy link
Copy Markdown
Owner

This issue is now resolved in v4.23.14.

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

github-actions Bot pushed a commit to modify-dev/harbor that referenced this pull request Oct 19, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [tsx](https://tsx.hirok.io) ([source](https://github.com/privatenumber/tsx)) | [`4.23.13` → `4.23.15`](https://renovatebot.com/diffs/npm/tsx/4.23.13/4.23.15) | ![age](https://developer.mend.io/api/mc/badges/age/npm/tsx/4.23.15?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/tsx/4.23.13/4.23.15?slim=true) |

---

> ⚠️ **Warning**
>
> Some dependencies could not be looked up. Check the [Dependency Dashboard](issues/99) for more information.

---

### Release Notes

<details>
<summary>privatenumber/tsx (tsx)</summary>

### [`v4.23.15`](https://github.com/privatenumber/tsx/releases/tag/v4.23.15)

[Compare Source](privatenumber/tsx@v4.23.14...v4.23.15)

##### Bug Fixes

- exclude bare builtins from namespace inheritance ([38e1588](privatenumber/tsx@38e1588))
- expose require.cache and require.extensions to tsImport CommonJS modules ([2da3407](privatenumber/tsx@2da3407))
- make namespaced register() overloads portable for declaration emit ([562c434](privatenumber/tsx@562c434))

***

This release is also available on:

- [npm package (@&#8203;latest dist-tag)](https://www.npmjs.com/package/tsx/v/4.23.15)

### [`v4.23.14`](https://github.com/privatenumber/tsx/releases/tag/v4.23.14)

[Compare Source](privatenumber/tsx@v4.23.13...v4.23.14)

##### Bug Fixes

- restore the CJS bridge namespace for Node 24 require(esm) under tsImport() ([#&#8203;802](privatenumber/tsx#802)) ([6e5236b](privatenumber/tsx@6e5236b))

***

This release is also available on:

- [npm package (@&#8203;latest dist-tag)](https://www.npmjs.com/package/tsx/v/4.23.14)

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - "before 9am on tuesday"
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC4xMzMuMCIsInVwZGF0ZWRJblZlciI6IjQ0LjE2Ny4wIiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCIsImxhYmVscyI6WyJkZXBlbmRlbmNpZXMiLCJyZW5vdmF0ZSJdfQ==-->

Reviewed-on: https://code.futo.org/harbor/harbor/pulls/1786
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.

tsImport() fails with ERR_MODULE_NOT_FOUND on Node 24 when a CommonJS-context .ts file imports an ESM dependency

2 participants