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

Misleading ERR_REQUIRE_CYCLE_MODULE when attempting to import from module if module was never installed #5290

Open
3 of 4 tasks
MaxKessler5 opened this issue Jan 30, 2025 · 1 comment · May be fixed by #5294
Open
3 of 4 tasks
Labels
status: accepting prs Mocha can use your help with this one! type: bug a defect, confirmed by a maintainer

Comments

@MaxKessler5
Copy link

Bug Report Checklist

  • I have read and agree to Mocha's Code of Conduct and Contributing Guidelines
  • I have searched for related issues and issues with the faq label, but none matched my issue.
  • I have 'smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, my usage of Mocha, or Mocha itself.
  • I want to provide a PR to resolve this

Expected

I forgot to npm install a library, and tried to test with Mocha. I expected an error about the missing module.

Actual

The error claims the missing module is a cyclical import. It is not cyclical, it is just missing

Minimal, Reproducible Example

Here is a gist that reproduces the problem using bash: https://gist.github.com/MaxKessler5/9d53a143df67ce1cd4dff9e9f03d2db4

The contents:

npm init -y
sed -i 's/"type": "commonjs"/"type": "module"/' package.json
echo "// Entry point for the application" > index.js
echo "import { cloneDeep } from "lodash-es";" >> index.js
sed -i 's/"test":.*/"test": "mocha"/' package.json
npm install mocha
mkdir test
echo "import "../index.js";" > test/test.js
npm test > output.txt

Versions

$ npx mocha --version
11.1.0

node_modules/.bin/mocha --version
(I'm on windows, not sure how to do this one)

node --version
v22.12.0

Additional Info

No response

@MaxKessler5 MaxKessler5 added status: in triage a maintainer should (re-)triage (review) this issue type: bug a defect, confirmed by a maintainer labels Jan 30, 2025
@JoshuaKGoldberg
Copy link
Member

👍 thanks for the report! Confirmed, I can reproduce this locally. I pushed up my repro here: https://github.com/JoshuaKGoldberg/repros/tree/mocha-missing-module-cyclic

On macOS with Node <=22.11.0:

 Exception during run: Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'lodash-es' imported from /Users/josh/repos/repros/index.js
    at packageResolve (node:internal/modules/esm/resolve:842:9)
    at moduleResolve (node:internal/modules/esm/resolve:915:18)
    at defaultResolve (node:internal/modules/esm/resolve:1132:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:557:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:526:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:249:38)
    at ModuleJob._link (node:internal/modules/esm/module_job:126:49) {
  code: 'ERR_MODULE_NOT_FOUND'
}

On macOS with Node >=22.12.0:

 Exception during run: Error [ERR_REQUIRE_CYCLE_MODULE]: Cannot require() ES Module /Users/josh/repos/repros/test/test.js in a cycle. (from /Users/josh/repos/repros/node_modules/mocha/lib/nodejs/esm-utils.js)
    at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:315:15)
    at loadESMFromCJS (node:internal/modules/cjs/loader:1414:24)
    at Module._compile (node:internal/modules/cjs/loader:1547:5)
    at Object..js (node:internal/modules/cjs/loader:1677:16)
    at Module.load (node:internal/modules/cjs/loader:1318:32)
    at Function._load (node:internal/modules/cjs/loader:1128:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:219:24)
    at Module.require (node:internal/modules/cjs/loader:1340:12)
    at require (node:internal/modules/helpers:138:16)
    at exports.requireOrImport (/Users/josh/repos/repros/node_modules/mocha/lib/nodejs/esm-utils.js:53:16)
    at async exports.loadFilesAsync (/Users/josh/repos/repros/node_modules/mocha/lib/nodejs/esm-utils.js:100:20)
    at async singleRun (/Users/josh/repos/repros/node_modules/mocha/lib/cli/run-helpers.js:162:3)
    at async exports.handler (/Users/josh/repos/repros/node_modules/mocha/lib/cli/run.js:375:5) {
  code: 'ERR_REQUIRE_CYCLE_MODULE'
}

ERR_REQUIRE_CYCLE_MODULE is kind of a weird error here. Agreed this is a bug.

I did a quick search for the error in the Node issue tracker and found:

nodejs/node#55516 looks like it might have links to a similar bug that was fixed in other libraries. Investigation required.

@JoshuaKGoldberg JoshuaKGoldberg added status: accepting prs Mocha can use your help with this one! and removed status: in triage a maintainer should (re-)triage (review) this issue labels Jan 30, 2025
@JoshuaKGoldberg JoshuaKGoldberg changed the title Misleading error message when attempting to import from module if module was never installed Misleading ERR_REQUIRE_CYCLE_MODULE when attempting to import from module if module was never installed Jan 30, 2025
Donbasilpeter added a commit to Donbasilpeter/mocha that referenced this issue Feb 6, 2025
…chajs#5290)  Use require to handle both ESM and CJS in the latest Node.js due to loadESMFromCJS function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: accepting prs Mocha can use your help with this one! type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants