Skip to content

@vitest/mocker 4.1.0: vite declared as optional peer dependency but imported unconditionally in dist/node.js #9915

@PersaPaul

Description

@PersaPaul

Describe the bug

Starting with @vitest/mocker 4.1.0, running vitest with @vitest/browser-playwright fails with ERR_MODULE_NOT_FOUND if vite is not explicitly installed in the project, even though vite is declared as an optional peer dependency.

Expected: Tests run (or a graceful error if vite is required)

Actual:

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'vite' imported from /path/to/node_modules/@vitest/browser-playwright/node_modules/@vitest/mocker/dist/node.js

Root cause

In @vitest/mocker/package.json, vite is declared as optional:

"peerDependencies": {
  "vite": "^6.0.0 || ^7.0.0 || ^8.0.0-0"
},
"peerDependenciesMeta": {
  "vite": {
    "optional": true
  }
}

However, dist/node.js appears to have an unconditional static import of vite, causing Node.js module resolution to fail immediately at load time when vite is not present.

This worked in 4.0.18

Workaround

Explicitly install vite as a devDependency:

npm install vite --save-dev

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-bjtmmvai?file=package.json&initialPath=__vitest__/

  1. Create a project with the following devDependencies:
    {
      "devDependencies": {
        "@vitest/browser-playwright": "4.1.0",
        "vitest": "4.1.0"
      }
    }
  2. Run npm install
  3. Run vitest

System Info

System:
    OS: macOS 26.3.1
    CPU: (12) arm64 Apple M4 Pro
    Memory: 8.55 GB / 48.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.22.1 - /usr/local/bin/node
    npm: 10.9.4 - /usr/local/bin/npm
  Browsers:
    Chrome: 146.0.7680.153
    Safari: 26.3.1
  npmPackages:
    @vitest/browser-playwright: 4.1 => 4.1.0 
    vitest: 4.1 => 4.1.0

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions