Skip to content

basuke/sveltekit-adapter-cloudflare-createrequire-repro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SvelteKit + adapter-cloudflare: worker fails to start with createRequire(import.meta.url) on Vite 8 (Rolldown)

Minimal reproduction: a SvelteKit app using @sveltejs/adapter-cloudflare on Vite 8 (which bundles Rolldown) crashes at Cloudflare Workers (workerd) startup when a CommonJS dependency in the SSR graph uses a Node builtin.

Repro

npm install --legacy-peer-deps   # @sveltejs/vite-plugin-svelte still declares vite ^6||^7 as a peer
npm run build
npx wrangler dev --local         # or: npx wrangler deploy
curl http://localhost:8787/

wrangler dev / deploy fails before serving any request:

Uncaught TypeError: The argument 'path' ... must be a file URL ... Received 'undefined'
  at createRequire (node:module)
[code: 10021] The Workers runtime failed to start.

npm run dev (Vite Node SSR) and npm run build succeed — it only fails in real workerd.

Cause

src/cjs-node-builtin.cjs is a CommonJS module that internally require("node:crypto") (the way dependencies like node-forge do). It is pulled into every page's SSR startup path via src/routes/+layout.server.js.

On Vite 8, Rolldown emits var __require = createRequire(import.meta.url) at the top of a shared chunk for CJS interop. import.meta.url is undefined in workerd, so the worker throws at startup. (.svelte-kit/output/server/chunks/chunk.js contains the offending line.)

@sveltejs/adapter-cloudflare does not set ssr.target: 'webworker', so the SSR build uses platform: 'node' and emits the createRequire shim. The old Astro adapter (@astrojs/cloudflare, on Vite 7 / Rollup) statically converts the same require() to an ESM import and does not hit this.

Related

  • Vite added platform: 'browser' for ssr.target: 'webworker' in vitejs/vite#21963.
  • The remaining "bundled CJS builtin require" gap is vitejs/vite#22618 / PR vitejs/vite#22619.
  • Once that lands, adapter-cloudflare setting ssr.target: 'webworker' would fix this generally.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors