Skip to content

fix: show both require() and import() errors when module loading fails#5795

Open
guoyangzhen wants to merge 4 commits intomochajs:mainfrom
guoyangzhen:fix/require-import-error-visibility
Open

fix: show both require() and import() errors when module loading fails#5795
guoyangzhen wants to merge 4 commits intomochajs:mainfrom
guoyangzhen:fix/require-import-error-visibility

Conversation

@guoyangzhen
Copy link
Copy Markdown

@guoyangzhen guoyangzhen commented Mar 18, 2026

Summary

Fixes #5235 (related to #5792 - edited by @mark-wiemer)

When requireModule() falls back to import() after require() 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:

Failed to load file via both require() and import().

[require() error]:
Cannot find module './test/something.test.ts' ...

[import() error]:
ERR_MODULE_NOT_FOUND ...

Context

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
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Mar 18, 2026

CLA Not Signed

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 18, 2026

👋 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 [x] checks on the following tasks from the PR template.

Repositories often provide a set of tasks that pull request authors are expected to complete. Those tasks should be marked as completed with a [x] in the pull request description. Please complete those tasks and mark the checks as [x] completed.

🗺️ This message was posted automatically by OctoGuide: a bot for GitHub repository best practices.

@mark-wiemer
Copy link
Copy Markdown
Member

@mochajs/maintenance-crew I like this idea, what are your thoughts?

@JoshuaKGoldberg
Copy link
Copy Markdown
Member

Ah, this old idea again. I'll post the old context in #5792 to keep discussion in the issue-side.

@mark-wiemer
Copy link
Copy Markdown
Member

@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

@guoyangzhen guoyangzhen marked this pull request as ready for review April 3, 2026 03:05
@guoyangzhen
Copy link
Copy Markdown
Author

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).

@guoyangzhen
Copy link
Copy Markdown
Author

@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!

@mark-wiemer
Copy link
Copy Markdown
Member

/easycla

@mark-wiemer
Copy link
Copy Markdown
Member

Thanks, we have had some issues with EasyCLA recently, we are working on them in private comms :)

@mark-wiemer
Copy link
Copy Markdown
Member

@lukaszgryglicki can you check this one out? See also #5803

@mark-wiemer mark-wiemer added status: blocked Waiting for something else to be resolved status: in triage a maintainer should (re-)triage (review) this issue labels Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: blocked Waiting for something else to be resolved status: in triage a maintainer should (re-)triage (review) this issue

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

🚀 Feature: if loading a file as CJS and ESM both fail, display both errors instead of just ESM

3 participants