Skip to content

"require is not defined" on the server, in dev mode only #19673

Open
@jakearchibald

Description

@jakearchibald

Describe the bug

jakearchibald/vite-srr-require-bug-demo@332d0ae - here's a reduced example of the issue, building on top of the Preact SSR template.

Here I'm using a plugin to import a module for SSR only. In this case, JSDOM. pnpm run build && pnpm run preview works fine. However, if I visit the app in pnpm run dev, I get the error ReferenceError: require is not defined.

Reproduction

jakearchibald/vite-srr-require-bug-demo@332d0ae

Steps to reproduce

  1. Check out https://github.com/jakearchibald/vite-srr-require-bug-demo/ and install dependencies.
  2. pnpm run dev
  3. Visit the server in a browser
  4. See ReferenceError: require is not defined
  5. Note that replacing step 2 with pnpm run build && pnpm run preview works fine.

System Info

System:
    OS: macOS 15.3.2
    CPU: (12) arm64 Apple M4 Pro
    Memory: 1.96 GB / 48.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 23.7.0 - /opt/homebrew/bin/node
    npm: 10.9.2 - /opt/homebrew/bin/npm
    pnpm: 10.4.1 - /opt/homebrew/bin/pnpm
    Watchman: 2025.02.17.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 134.0.6998.89
    Chrome Canary: 136.0.7075.0
    Safari: 18.3.1
  npmPackages:
    vite: ^6.1.1 => 6.2.2

Used Package Manager

pnpm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions