Repository navigation
fix: change OR to AND in TS file extension check for ERR_UNKNOWN_FILE_EXTENSION - #5811
Closed
mark-wiemer with Copilot wants to merge 2 commits into
Closed
mark-wiemer with Copilot wants to merge 2 commits into
mark-wiemer with Copilot wants to merge 2 commits into
Conversation
|
…_EXTENSION Fixes #5494. The condition in requireModule was using || (OR) when it should use && (AND). This caused any .ts file that failed both require() and import() to always throw the require() error, even when the import() error was more informative (e.g., a runtime error vs ERR_REQUIRE_ASYNC_MODULE). Co-authored-by: mark-wiemer <7833360+mark-wiemer@users.noreply.github.com> Agent-Logs-Url: https://github.com/mochajs/mocha/sessions/ddee8dff-9212-4d12-9857-f9321ec72463
Copilot
AI
changed the title
[WIP] Fix bug with Mocha v11.7.3 and v11.7.4 ERR_REQUIRE_ESM error
fix: change OR to AND in TS file extension check for ERR_UNKNOWN_FILE_EXTENSION
Mar 24, 2026
Member
|
Meh, bad bot. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR #5408 introduced a regression where the condition guarding TS file error handling used
||instead of&&. This caused any.tsfile that fails bothrequire()andimport()to always surface the unhelpfulrequireErr, masking the actual runtime error fromimport().The PR description stated the intent correctly ("if the file is
.cts,.mts, or.tsand the error isERR_UNKNOWN_FILE_EXTENSION"), but the code used OR.Changes
lib/nodejs/esm-utils.js:||→&&in therequireModuleerror handlertest/node-unit/fixtures/esm-ts-runtime-error/: New fixture — a.tsfile with top-levelawaitin an ESM package that throws a runtime error, triggering the exact bug scenario (require()→ERR_REQUIRE_ASYNC_MODULE,import()→ actual error)test/node-unit/esm-utils.spec.js: Test asserting theimportErris surfaced, notrequireErrOriginal prompt
This section details on the original issue you should resolve
<issue_title>🐛 Bug: Mocha v11.7.3 and v11.7.4 fails with error ERR_REQUIRE_ESM</issue_title>
<issue_description>### Bug Report Checklist
faqlabel, but none matched my issue.Expected
Unit tests runs as expected
Actual
Unit tests fail with error ERR_REQUIRE_ESM
After some investigation, this problem is the same as #5387 which was fixed in 11.7.1 and 11.7.2 but is broken again in 11.7.3 and 11.7.4 .
The problem has been re-introduced with PR #5408 and the issue might be in
lib/nodejs/esm-utils.jsline 108 in the PR, the condition readsbut in the PR description it is
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.
or in the code, and in description.
Note : While making the minimal reproducible example, it appears that if the imported module has no async bit the tests are not run and fail silently.
Minimal, Reproducible Example
Versions
v11.7.3, v11.7.4
Nodejs v24.3.0
Additional Info
No response</issue_description>
<agent_instructions>## Postmortem
I never fully wrapped my head around the exact problem here. Why was Node loading modules in a different way based on whether
nychad been enabled? How can we track that to diagnose future issues?I'll be updating this comment as I reflect more, I plan to reproduce the issue in excruciating detail so that I can summarize it precisely.
Open questions
References
ERR_UNKNOWN_FILE_EXTENSION#5393@copilot draft a report about why this error happened, it's quite complex. Organize sources so that the report is readable.</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@mark-wiemer Thanks guys, let me confirm with @JoshuaKGoldberg but changing "or" to "and" looks like a good candidate for a fix @mark-wiemer I'll note this bug only repros if Node is already in "module" mode. For Mocha, since it doesn't have `"type": "module"` in its package.json, that's not the case. The repro commit above does work, and changing the mentioned "or" to an "and" does fix it. This looks like the intended fix, I'll just need to find a way to write an automated test for this within Mocha! @mark-wiemer Ref: - https://github.com//issues/5361#issuecomment-3368708708 - https://nodejs.org/docs/v22.20.0/api/module.html#customization-hooks - https://github.com/mochajs/mocha/compare/fix-5361-esm-loader @JoshuaKGoldberg 👍 Great investigation @mark-wiemer, I concur!🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.