Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Donbasilpeter
Copy link

@Donbasilpeter Donbasilpeter commented Feb 6, 2025

PR Checklist

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

…chajs#5290)  Use require to handle both ESM and CJS in the latest Node.js due to loadESMFromCJS function.
Copy link

linux-foundation-easycla bot commented Feb 6, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a 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! 🚀

}
}
};


Copy link
Member

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.

Suggested change

Copy link
Member

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.

Copy link
Author

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 JoshuaKGoldberg added the status: waiting for author waiting on response from OP - more information needed label Feb 6, 2025
@Donbasilpeter
Copy link
Author

Donbasilpeter commented Feb 6, 2025

@JoshuaKGoldberg I have looked in to the test cases.

The only test case that fails :
https://github.com/mochajs/mocha/blob/main/test/integration/plugins/root-hooks.spec.js#L138

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

@JoshuaKGoldberg
Copy link
Member

😄 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.
@Donbasilpeter
Copy link
Author

Donbasilpeter commented Feb 10, 2025

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

@Donbasilpeter Donbasilpeter changed the title Fix: Resolve ERR_REQUIRE_CYCLE_MODULE for never installed modules (#5290) Fix: Resolve ERR_REQUIRE_CYCLE_MODULE for never installed modules Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for author waiting on response from OP - more information needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Misleading ERR_REQUIRE_CYCLE_MODULE when attempting to import from module if module was never installed
2 participants