vitest-dev / vitest-dev/vitest

Types exports from vitest/browser are not found by typescript when using npm workspaces

Open
#10,280 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pending triage
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

Describe the bug

In an npm workspaces monorepo, page, userEvent, and server are not exported from vitest/browser when typechecking with tsc. The issue is a TypeScript module resolution failure: vitest/browser/context.d.ts re-exports from @vitest/browser-playwright/context, but when vitest is hoisted to the root node_modules/ and @vitest/browser-playwright is installed only in a workspace's local node_modules/,
TypeScript cannot resolve that re-export from vitest's location. The // @ts-ignore comment above the export silently swallows the resolution failure, so instead of an error on the library file, all the exports from @vitest/browser-playwright/context simply disappear — and user code gets Module '"vitest/browser"' has no exported member 'page'.

This is the expected npm layout for optional peer dependencies: npm deliberately does not hoist @vitest/browser-playwright to root (where vitest lives) because it was not explicitly requested there. The bug is that vitest's type declarations depend on being able to reach their optional peer packages at type-check time, which breaks in any monorepo where those peers are co-located with the workspace rather than
with vitest itself.

The bug only triggers when npm produces this split layout. If npm happens to co-locate vitest and @vitest/browser-playwright in the same node_modules/ directory (for example because a version mismatch forces a local copy of vitest into the workspace), TypeScript can resolve the chain and the error disappears. This makes the bug sensitive to npm hoisting decisions, which can change across installs as dependency versions are bumped.

Reproduction

https://github.com/julienw/vitest-types-issue

git clone https://github.com/julienw/vitest-types-issue
npm ci
cd packages/frontend
npx tsc

I get these errors:

../../node_modules/vitest/dist/browser.d.ts:34:13 - error TS2304: Cannot find name 'BufferEncoding'.

34  encoding?: BufferEncoding;
               ~~~~~~~~~~~~~~

../../node_modules/vitest/dist/browser.d.ts:38:37 - error TS2304: Cannot find name 'BufferEncoding'.

38  readFile: (path: string, options?: BufferEncoding | FsOptions) => Promise<string>;
                                       ~~~~~~~~~~~~~~

../../node_modules/vitest/dist/browser.d.ts:39:55 - error TS2304: Cannot find name 'BufferEncoding'.

39  writeFile: (path: string, content: string, options?: BufferEncoding | (FsOptions & {
                                                         ~~~~~~~~~~~~~~

test/example.test.ts:2:10 - error TS2305: Module '"vitest/browser"' has no exported member 'page'.

2 import { page, userEvent, server } from "vitest/browser";
           ~~~~

test/example.test.ts:2:16 - error TS2305: Module '"vitest/browser"' has no exported member 'userEvent'.

2 import { page, userEvent, server } from "vitest/browser";
                 ~~~~~~~~~

test/example.test.ts:2:27 - error TS2305: Module '"vitest/browser"' has no exported member 'server'.

2 import { page, userEvent, server } from "vitest/browser";
                            ~~~~~~


Found 6 errors in 2 files.

Errors  Files
     3  ../../node_modules/vitest/dist/browser.d.ts:34
     3  test/example.test.ts:2

(BufferEncoding errors are a separate issue, already suppressed by skipLibCheck: true in most projects.)

System Info
System:
    OS: Linux 6.12 Debian GNU/Linux 13 (trixie) 13 (trixie)
    CPU: (16) x64 11th Gen Intel(R) Core(TM) i9-11900H @ 2.50GHz
    Memory: 48.89 GB / 62.53 GB
    Container: Yes
    Shell: 5.2.37 - /bin/bash
  Binaries:
    Node: 24.15.0 - /home/julien/.nvm/versions/node/v24.15.0/bin/node
    Yarn: 1.22.22 - /usr/bin/yarn
    npm: 11.12.1 - /home/julien/.nvm/versions/node/v24.15.0/bin/npm
    pnpm: 10.9.0 - /home/julien/.local/share/pnpm/pnpm
    bun: 1.0.29 - /home/julien/.bun/bin/bun
    Deno: 2.0.0 - /home/julien/.cargo/bin/deno
  Browsers:
    Chrome: 147.0.7727.137
    Chromium: 147.0.7727.116
    Firefox: 140.10.0esr
    Firefox Developer Edition: 140.10.0esr
  npmPackages:
    @vitest/browser-playwright: 4.1.5 => 4.1.5 


(the command doesn't give the version for `vitest` in monorepos, but it's 4.1.5)
Used Package Manager

npm

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction repository and run npm ci, then npx tsc from packages/frontend. Inspect vitest/dist/browser.d.ts and vitest/browser/context.d.ts, especially the re-export from @vitest/browser-playwright/context. Done means TypeScript resolves page, userEvent, and server from vitest/browser in the split npm workspace layout without the reported missing-export errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
testing-qa, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.