Skip to content

ssr.target: 'webworker' still defaults Rolldown SSR builds to platform: 'node', emitting createRequire(import.meta.url) that crashes on Workers #21969

Description

@jmalmo

Describe the bug

For SSR builds with ssr.target: 'webworker', Vite still defaults build.rolldownOptions.platform to 'node' for every server build. This causes Rolldown to emit createRequire(import.meta.url) for CJS interop, which crashes on Cloudflare Workers / workerd because import.meta.url is undefined in bundled worker modules.

The worker fails during module initialization before any request is handled:

TypeError: The argument 'path' must be a file URL object, a file URL string,
or an absolute path string. Received 'undefined'
  at createRequire (node:module:34:15)

Vite already treats ssr.target === 'webworker' as a special case elsewhere — ssr.resolve.conditions defaults to webworker/browser-style conditions, resolveEnvironmentOptions() threads isSsrTargetWebworkerEnvironment into resolve + keepProcessEnv, and resolveRolldownOptions() special-cases SSR webworker output defaults. But resolveBuildEnvironmentOptions() still hard-defaults all server builds to platform: 'node'.

I am happy to take a stab at a PR if the team points us in the right direction.

Reproduction

https://github.com/jmalmo/vite-ssr-webworker-createrequire-repro

Steps to reproduce

  1. git clone https://github.com/jmalmo/vite-ssr-webworker-createrequire-repro && cd vite-ssr-webworker-createrequire-repro
  2. pnpm install
  3. pnpm run build
  4. pnpm run check — shows createRequire(import.meta.url) in output
  5. pnpm run preview — crashes with TypeError: The argument 'path' must be a file URL object...

System Info

System:
  OS: macOS 15.6.1 (Darwin 24.6.0)
  CPU: Apple M4 Max
  Memory: 48 GB
Binaries:
  Node: 22.13.1
  bun: 1.3.3
  pnpm: 10.32.1
npmPackages:
  vite: npm:rolldown-vite@^7.3.1 => 7.3.1
  rolldown: 1.0.0-rc.10 (transitive via rolldown-vite)

Used Package Manager

bun

Logs

Click to expand!
# Build succeeds but output contains createRequire:
$ bun run web:build && grep "createRequire" apps/web/.output/server/_chunks/jsx-runtime.mjs
import { createRequire } from "node:module";
var __require = /* @__PURE__ */ createRequire(import.meta.url);

# Worker crashes at startup:
$ npx wrangler dev .output/server/index.mjs
Uncaught TypeError: The argument 'path' must be a file URL object,
a file URL string, or an absolute path string. Received 'undefined'
  at createRequire (node:module:34:15)
  at null.<anonymous> (jsx-runtime.mjs:34:33)

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: ssrp3-downstream-blockerBlocking the downstream ecosystem to work properly (priority)

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions