Skip to content

fix: use original require() error for TS files if ERR_UNKNOWN_FILE_EXTENSION - #5408

Merged
JoshuaKGoldberg merged 9 commits into
mochajs:mainfrom
JoshuaKGoldberg:require-module-syntax-throw
Sep 30, 2025
Merged

fix: use original require() error for TS files if ERR_UNKNOWN_FILE_EXTENSION#5408
JoshuaKGoldberg merged 9 commits into
mochajs:mainfrom
JoshuaKGoldberg:require-module-syntax-throw

Conversation

@JoshuaKGoldberg

@JoshuaKGoldberg JoshuaKGoldberg commented Jul 29, 2025

Copy link
Copy Markdown
Member

PR Checklist

Overview

Adds in a special-case try/catch -> throw. If the file being imported is a .cts, .mts, or .ts and the caught error from import() is for an unknown file extension, we know it's from a Node.js version that doesn't support those file extensions. In that case we'll want to throw the original informative require() error per #5393.

💖

@JoshuaKGoldberg JoshuaKGoldberg changed the title fix: use original require() error if ERR_UNKNOWN_FILE_EXTENSION fix: use original require() error for TS files if ERR_UNKNOWN_FILE_EXTENSION Jul 29, 2025
Comment thread lib/nodejs/esm-utils.js
Comment thread lib/nodejs/esm-utils.js Outdated
@JoshuaKGoldberg JoshuaKGoldberg added the status: blocked Waiting for something else to be resolved label Aug 5, 2025
Co-authored-by: Mark Wiemer <7833360+mark-wiemer@users.noreply.github.com>
@JoshuaKGoldberg JoshuaKGoldberg removed the status: blocked Waiting for something else to be resolved label Sep 1, 2025
Comment thread lib/nodejs/esm-utils.js Outdated
Comment thread test/node-unit/esm-utils.spec.js
@JoshuaKGoldberg

Copy link
Copy Markdown
Member Author

cc @mark-wiemer - none of the test failures are unique to this PR. I filed #5467.

@JoshuaKGoldberg
JoshuaKGoldberg merged commit ebdbc48 into mochajs:main Sep 30, 2025
89 of 93 checks passed
@JoshuaKGoldberg
JoshuaKGoldberg deleted the require-module-syntax-throw branch September 30, 2025 13:58
lennonnikolas pushed a commit to lennonnikolas/mocha that referenced this pull request Jan 24, 2026
…TENSION (mochajs#5408)

* fix: use original require() error if ERR_UNKNOWN_FILE_EXTENSION

* Update lib/nodejs/esm-utils.js

Co-authored-by: Mark Wiemer <7833360+mark-wiemer@users.noreply.github.com>

* Add in comments

* test: add broken-syntax.ts fixture and test

* chore: reset a change to main

* test: slightly improve name

---------

Co-authored-by: Mark Wiemer <7833360+mark-wiemer@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Bug: original require() errors are masked with an unrelated ERR_UNKNOWN_FILE_EXTENSION

2 participants