Skip to content

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

Closed

Description

@MaxKessler5

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

Activity

  1. added
    status: in triagea maintainer should (re-)triage (review) this issue
    type: buga defect, confirmed by a maintainer
    on Jan 30, 2025
  2. JoshuaKGoldberg commented on Jan 30, 2025

    @JoshuaKGoldberg
    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.

  3. added and removed
    status: in triagea maintainer should (re-)triage (review) this issue
    on Jan 30, 2025
  4. 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
  5. joyeecheung commented on Feb 23, 2025

    @joyeecheung

    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

     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'
    }
    
  6. 9 remaining items

  7. 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
  8. self-assigned this
    on Sep 1, 2025
  9. mark-wiemer commented on Sep 1, 2025

    @mark-wiemer
    Member

    Going to re-evaluate this one as it may have been fixed with recent Node ESM work

  10. mark-wiemer commented on Sep 1, 2025

    @mark-wiemer
    Member

    Fixed with 11.7.2 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

status: accepting prsMocha can use your help with this one!type: buga defect, confirmed by a maintainer

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions