Repository navigation
SvelteKit: a third-party package's void/db gets the D1 client in dev, and pg/MySQL builds inline workerd-only dependency code #420
Description
Problem
a. A package that imports void/db gets the D1 client in dev
In a SvelteKit app with database: 'pg' (or MySQL), the app's own void/db is the generated pg client. A package in node_modules that imports void/db gets Void's static D1 client instead. Using it throws:
db: Missing Cloudflare binding 'DB'. This app connects through HYPERDRIVE or DATABASE_URL, but its server
bundle loaded the D1 client. In Nuxt or Analog, add 'void/nitro' to the Nitro modules.
The message also points SvelteKit users to Nuxt and Analog, which does not help them.
app route ── import 'void/db' ──> Vite SSR graph ──> generated pg client (ok)
xa-db-lib ── import 'void/db' ──> Node (package is external) ──> dist/runtime/db.mjs (D1 client)
b. pg/MySQL builds inline workerd-only dependency code (risk)
For SvelteKit pg/MySQL builds, #413 builds Kit's ssr environment for workerd: resolve.noExternal: true, workerd/worker conditions and neutral output. Kit's analyse step then loads that output in Node. A dependency whose workerd build statically imports a cloudflare: module would now be bundled into that output, and Node cannot load it (ERR_UNSUPPORTED_ESM_URL_SCHEME). Before #413 such a dependency stayed external, and Node loaded its Node build.
This was seen only with a synthetic package (exports: { workerd: './cf.js', default: './node.js' }, where cf.js has import { connect } from 'cloudflare:sockets'). The server entry then started with import "cloudflare:sockets". No real package has been shown to break. pg is fine: pg-cloudflare imports cloudflare:sockets lazily.
Repro
a.
- SvelteKit app,
void.config.tswithdatabase: 'pg', a pg schema, andDATABASE_URLin.env. - Add
node_modules/xa-db-lib/index.js:import { db } from 'void/db'; export function libDb() { try { return { execute: typeof db.execute }; } catch (e) { return { error: String(e) }; } }
- Add
src/routes/api/lib/+server.ts, which returns{ app: typeof db.execute, lib: libDb() }. vp dev, request/api/lib:{"app":{"execute":"function","run":"undefined"},"lib":{"error":"Error: db: Missing Cloudflare binding 'DB'. ..."}}
b. Add a dependency whose workerd export statically imports cloudflare:sockets, import it from server code in a SvelteKit pg app, and run vp build.
Cause
- a. Vite's SSR graph externalizes a package that resolves into
node_modules. Void inlines onlyvoiditself (packages/void/src/plugin-framework.ts:232,ssr.noExternal: ['void']). The external package'svoid/dbgoes to the package export./dist/runtime/db.mjs, the static D1 client (packages/void/src/runtime/db.ts:59-70).packages/void/AGENTS.md:101notes that such packages load their own env module copies;void/dbhas the same limit. - b.
packages/void/src/plugin-framework.ts:310-330(void:sveltekit-worker-build):noExternal: trueat:325, conditions at:322,platform: 'neutral'at:329.
Suggested fix
- a. In SvelteKit dev, inline packages that import
void/*(for example, find them in the dependency graph and add them tossr.noExternal). At least, make the D1 client's error name the real cause for SvelteKit. - b. Inline only the driver chain that
void/dbneeds (void,drizzle-orm,pg,pg-*,mysql2and their dependencies), and leave other dependencies to Kit's Node resolution. Or document the limit. Check real packages first, for examplepostgres(itscloudflarebuild).
Evidence
- a. Reproduced at the head of fix(void): make PostgreSQL and MySQL void/db work across frameworks, builds and deploys #413 with a workspace-linked
void(response above). fix(void): make PostgreSQL and MySQL void/db work across frameworks, builds and deploys #413 makes the app's ownvoid/dbwork with an installedvoid; it does not change a third-party package's import, and it adds the Nuxt/Analog wording to the error. - b. Synthetic probe only, built with the head of fix(void): make PostgreSQL and MySQL void/db work across frameworks, builds and deploys #413. The SvelteKit build plugin is new in fix(void): make PostgreSQL and MySQL void/db work across frameworks, builds and deploys #413 (
bd1705f26); before it, pg/MySQL SvelteKit Workers failed at request time instead.
🤖 Generated with Claude Code
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
fix(void): bundle installed void packages for the Node target and Sve…
fix(void): bundle installed void packages for the Node target and Sve…
fix(void): bundle installed void packages for the Node target and Sve…
fix(void): bundle installed void packages for the Node target and Sve…
fix(void): bundle installed void packages for the Node target and Sve…