You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
🐛 Bug: Misleading ERR_REQUIRE_CYCLE_MODULE when attempting to import from module if module was never installed #5290
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
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:
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[/+]on Jan 30, 2025
Exception during run: Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'lodash-es' imported from /Users/joyee/projects/repros/index.js
at Object.getPackageJSONURL (node:internal/modules/package_json_reader:267:9)
at packageResolve (node:internal/modules/esm/resolve:768:81)
at moduleResolve (node:internal/modules/esm/resolve:854:18)
at defaultResolve (node:internal/modules/esm/resolve:984:11)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:765:12)
at #cachedDefaultResolve (node:internal/modules/esm/loader:689:25)
at ModuleLoader.resolve (node:internal/modules/esm/loader:672:38)
at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:289:38)
at ModuleJob._link (node:internal/modules/esm/module_job:137:49) {
code: 'ERR_MODULE_NOT_FOUND'
}
changed the title [-]Misleading ERR_REQUIRE_CYCLE_MODULE when attempting to import from module if module was never installed[/-][+]🐛 Bug: Misleading ERR_REQUIRE_CYCLE_MODULE when attempting to import from module if module was never installed[/+]on Sep 1, 2025
Activity
JoshuaKGoldberg commentedon Jan 30, 2025
👍 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:On macOS with Node
>=22.12.0:ERR_REQUIRE_CYCLE_MODULEis 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:
ERR_REQUIRE_CYCLE_MODULEforrequire(esm)nodejs/node#55516nodejs/node#55516 looks like it might have links to a similar bug that was fixed in other libraries. Investigation required.
[-]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[/+]Fix: Resolve ERR_REQUIRE_CYCLE_MODULE for never installed modules (mo…
joyeecheung commentedon Feb 23, 2025
I have a fix in the upstream nodejs/node#57187 - locally this makes https://github.com/JoshuaKGoldberg/repros/tree/mocha-missing-module-cyclic produce this error again
module: handle cached linked async jobs in require(esm)
module: handle cached linked async jobs in require(esm)
module: handle cached linked async jobs in require(esm)
9 remaining items
module: handle cached linked async jobs in require(esm)
module: handle cached linked async jobs in require(esm)
module: handle cached linked async jobs in require(esm)
module: handle cached linked async jobs in require(esm)
module: handle cached linked async jobs in require(esm)
module: handle cached linked async jobs in require(esm)
[-]Misleading ERR_REQUIRE_CYCLE_MODULE when attempting to import from module if module was never installed[/-][+]🐛 Bug: Misleading ERR_REQUIRE_CYCLE_MODULE when attempting to import from module if module was never installed[/+]mark-wiemer commentedon Sep 1, 2025
Going to re-evaluate this one as it may have been fixed with recent Node ESM work
mark-wiemer commentedon Sep 1, 2025
Fixed with 11.7.2 :)