-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Resolve ERR_REQUIRE_CYCLE_MODULE for never installed modules #5294
base: main
Are you sure you want to change the base?
Conversation
…chajs#5290) Use require to handle both ESM and CJS in the latest Node.js due to loadESMFromCJS function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for getting this started! 🚀
} | ||
} | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Style] Nit: unrelated change, let's revert.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Testing] This is important code, could you please add some tests? I'm not confident reviewing without the new behavior being tested.
You can find examples of tests covering this file's behaviors in the last few PRs that touched this file in the Git blame.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure will do that . Thank you
@JoshuaKGoldberg I have looked in to the test cases. The only test case that fails : node js consider this as Ambiguous file but from versions >=node 2.7 this is not the case. https://nodejs.org/en/blog/release/v22.7.0#module-syntax-detection-is-now-enabled-by-default the function doesn't handle this case and returns an ERR_REQUIRE_CYCLE_MODULE Error, the test case just expects this error and passes. but it is not the actual expected behaviour. i jsut did a camparison which can be found below : https://docs.google.com/spreadsheets/d/1T9AsIWK5tLsnqicjGuh9miyPGetemOgxLzGzAK3ZBJA/edit?usp=sharing please review and feedback so that I can start updating the test case |
😄 It's been a while since I've seen test cases sent in a spreadsheet. Those seem reasonable, thanks for thinking so deeply on them. It's hard to say for sure until we see them implemented in PR review though. |
…hey are no longer considered ambiguous after Node.js 22.7.
I also wanted to add that the test case to produce the specific issue #5920 is already present in the code: https://github.com/mochajs/mocha/blob/main/test/integration/esm.spec.js It can be reproduced by running with v22.12.0 |
PR Checklist
status: accepting prs
Overview
The requireOrImport function was originally set up to try importing a file as an ES module first, and if that failed, it would fall back to require. However, since Node.js version 22.12, the require function now automatically handles both ES modules (ESM) and CommonJS (CJS). This caused an issue where calling require after a failed import could lead to a cyclic dependency error.
in the updated code I have used require first and if fails with requireErr.code === 'ERR_REQUIRE_ESM' which only happens if version is <22.12.0 and and target file is esm ,then use the older method.
https://nodejs.org/en/blog/release/v22.12.0
https://joyeecheung.github.io/blog/2024/03/18/require-esm-in-node-js