fix: show both require() and import() errors when module loading fails#5795
fix: show both require() and import() errors when module loading fails#5795guoyangzhen wants to merge 4 commits intomochajs:mainfrom
Conversation
When requireModule() falls back to import() after require() fails, and both fail, the current heuristics often pick the less helpful error to display. This change shows both errors with clear labels so users can diagnose the actual root cause. Fixes mochajs#5792
|
|
👋 Hi @guoyangzhen, thanks for the pull request! A scan flagged a concern with it. Could you please take a look? [pr-task-completion] This PR's body is missing
Repositories often provide a set of tasks that pull request authors are expected to complete. Those tasks should be marked as completed with a
|
|
@mochajs/maintenance-crew I like this idea, what are your thoughts? |
|
Ah, this old idea again. I'll post the old context in #5792 to keep discussion in the issue-side. |
|
@guoyangzhen unit tests for this change will be required before we merge :) we are discussing the core of this in #5792 currently and may move the convo to #5235 soon |
|
Hi @mark-wiemer, I've added unit tests as requested. The test creates a fixture that fails on require() with a custom error, stubs doImport to also fail, and verifies the combined error (ERR_MOCHA_MODULE_LOAD_FAILED) is thrown with both error messages included. Commits: fixture (c8ec45f) and test (b10432e). |
|
@mark-wiemer I've signed the EasyCLA — could you please re-trigger the check? Also, I noticed the CLA seems to need re-signing for each new PR. Thanks! |
|
/easycla |
|
Thanks, we have had some issues with EasyCLA recently, we are working on them in private comms :) |
|
@lukaszgryglicki can you check this one out? See also #5803 |
Summary
Fixes #5235 (related to #5792 - edited by @mark-wiemer)
When
requireModule()falls back toimport()afterrequire()fails, and both methods fail, the current heuristics often select the less helpful error to display. This is especially problematic with ts-node + CommonJS setups on Node.js 22+.Changes
Instead of guessing which error is more relevant, this PR shows both errors with clear labels when neither matches the known special cases (#5393, #5411). This lets users see the full picture and diagnose the actual root cause.
Example output
Before: only shows the generic import error (e.g., ERR_MODULE_NOT_FOUND)
After:
Context
ERR_UNKNOWN_FILE_EXTENSION#5393) and exports mismatches (🐛 Bug: Unhelpful error message when a default import is made from a module without a default export #5411) are preservedERR_MOCHA_MODULE_LOAD_FAILEDerror code makes it easy to identify combined errorserror.causefor programmatic access