sveltejs / sveltejs/kit

SvelteKit Vite plugin crash with remote functions and vitest 4

Open
#15,036 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

vite
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

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

> basic@0.0.1 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/pg@8.16.3/node_modules/pg/lib/index.js:5:16)
      at ESModulesEvaluator.runInlinedModule (file:///Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/vite@7.2.6/node_modules/vite/dist/node/module-runner.js:913:161)
      at SSRCompatModuleRunner.directRequest (file:///Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/vite@7.2.6/node_modules/vite/dist/node/module-runner.js:1146:80)
      at SSRCompatModuleRunner.directRequest (file:///Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/vite@7.2.6/node_modules/vite/dist/node/chunks/config.js:15110:22)
      at SSRCompatModuleRunner.cachedRequest (file:///Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/vite@7.2.6/node_modules/vite/dist/node/module-runner.js:1053:73)
      at eval (/Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/pg@8.16.3/node_modules/pg/esm/index.mjs:15:31)
      at ESModulesEvaluator.runInlinedModule (file:///Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/vite@7.2.6/node_modules/vite/dist/node/module-runner.js:913:3)
      at SSRCompatModuleRunner.directRequest (file:///Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/vite@7.2.6/node_modules/vite/dist/node/module-runner.js:1146:59)
      at SSRCompatModuleRunner.directRequest (file:///Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/vite@7.2.6/node_modules/vite/dist/node/chunks/config.js:15110:22)
      at SSRCompatModuleRunner.cachedRequest (file:///Users/dimfeld/Documents/projects/vitest4-remotefunctions/node_modules/.pnpm/vite@7.2.6/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/pg@8.16.3/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/vite@7.2.6/node_modules/vite/dist/node/chunks/config.js:15110:22
 ❯ eval node_modules/.pnpm/pg@8.16.3/node_modules/pg/esm/index.mjs:15:31
 ❯ SSRCompatModuleRunner.directRequest node_modules/.pnpm/vite@7.2.6/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)

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

Clone the linked reproduction, inspect src/routes/query.remote.ts and src/routes/page.test.ts, then run pnpm install && pnpm test with Vitest 4. Start at the vite-plugin-sveltekit-remote failure and the pg import path shown in the logs. Done means the test suite runs without the require is not defined error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, postgresql, vite
Domain
testing-qa, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.