Skip to content

SvelteKit Vite plugin crash with remote functions and vitest 4 #15036

@dimfeld

Description

@dimfeld

Describe the bug

When:

  • You are running Vitest 4
  • you have a test that runs in jsdom mode (not 100% sure this is necessary to reproduce)
  • the test imports a component
  • that component imports a remote function
  • that remote function imports (directly or indirectly) the pg package

then the test will fail to run with the logs below. Looks like some issue with the export map conditions?

Same code works fine on Vitest 3.

Reproduction

https://github.com/dimfeld/vitest4-remotefunctions

pnpm install && pnpm test

Logs

~/Doc/pro/vitest4-remotefunctions ❯ pnpm test

> [email protected] test /Users/dimfeld/Documents/projects/vitest4-remotefunctions
> vitest run

The following Vite config options will be overridden by SvelteKit:
  - base

 RUN  v4.0.15 /Users/dimfeld/Documents/projects/vitest4-remotefunctions

11:05:30 PM [vite] (ssr) Error when evaluating SSR module /Users/dimfeld/Documents/projects/vitest4-remotefunctions/src/routes/query.remote.ts: require is not defined
      at eval (/Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/[email protected]/node_modules/pg/lib/index.js:5:16)
      at ESModulesEvaluator.runInlinedModule (file:///Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:913:161)
      at SSRCompatModuleRunner.directRequest (file:///Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:1146:80)
      at SSRCompatModuleRunner.directRequest (file:///Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/config.js:15110:22)
      at SSRCompatModuleRunner.cachedRequest (file:///Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:1053:73)
      at eval (/Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/[email protected]/node_modules/pg/esm/index.mjs:15:31)
      at ESModulesEvaluator.runInlinedModule (file:///Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:913:3)
      at SSRCompatModuleRunner.directRequest (file:///Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:1146:59)
      at SSRCompatModuleRunner.directRequest (file:///Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/config.js:15110:22)
      at SSRCompatModuleRunner.cachedRequest (file:///Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/module-runner.js:1053:73)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  src/routes/page.test.ts [ src/routes/page.test.ts ]
ReferenceError: require is not defined
  Plugin: vite-plugin-sveltekit-remote
  File: /Users/dimfeld/Documents/projects/vitest4-remotefunctions/src/routes/query.remote.ts
 ❯ eval node_modules/.pnpm/[email protected]/node_modules/pg/lib/index.js:5:16
      3| const Client = require('./client')
      4| const defaults = require('./defaults')
      5| const Connection = require('./connection')
       |                ^
      6| const Result = require('./result')
      7| const utils = require('./utils')
 ❯ SSRCompatModuleRunner.directRequest node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/config.js:15110:22
 ❯ eval node_modules/.pnpm/[email protected]/node_modules/pg/esm/index.mjs:15:31
 ❯ SSRCompatModuleRunner.directRequest node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/config.js:15110:22

System Info

System:
    OS: macOS 26.1
    CPU: (16) arm64 Apple M3 Max
    Memory: 4.91 GB / 128.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.11.1 - /opt/homebrew/opt/node@24/bin/node
    Yarn: 1.22.22 - /Users/dimfeld/Library/pnpm/yarn
    npm: 11.6.2 - /opt/homebrew/opt/node@24/bin/npm
    pnpm: 10.24.0 - /opt/homebrew/bin/pnpm
    bun: 1.3.4 - /opt/homebrew/bin/bun
    Deno: 2.5.6 - /opt/homebrew/bin/deno
    Watchman: 2025.11.10.00 - /opt/homebrew/bin/watchman
  Browsers:
    Brave Browser: 143.1.85.111
    Firefox: 134.0.1
    Safari: 26.1
  npmPackages:
    @sveltejs/adapter-auto: ^7.0.0 => 7.0.0
    @sveltejs/kit: ^2.5.27 => 2.49.1
    @sveltejs/vite-plugin-svelte: ^6.2.1 => 6.2.1
    svelte: ^5 => 5.45.6
    vite: ^7.0.0 => 7.2.6

Severity

blocking an upgrade

Additional Information

(blocking an upgrade to Vitest 4, not an upgrade of Svelte/Kit)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions