How do I work with local D1 in a SvelteKit project?

I've created a new SvelteKit project and can successfully interact with hosted D1 after following the guide https://developers.cloudflare.com/d1/examples/d1-and-sveltekit/. Using pnpm dev to launch the SvelteKit local dev environment as standard works for the hosted D1. However I would like to develop locally also. The guide advises to pass --d1 BINDING_NAME=DATABASE_ID to wrangler dev (e.g., npx wrangler dev --d1 DB=example-uuid-here) but this results in ✘ [ERROR] Unknown argument: d1 (which aligns with wrangler's docs https://developers.cloudflare.com/workers/wrangler/commands/#dev, no d1 option exists) I have the following config:
# wrangler.toml
name = "example"
compatibility_date = "2024-04-23"

[[d1_databases]]
binding = "DB"
database_name = "example-prod"
database_id = "example-uuid-here"
# wrangler.toml
name = "example"
compatibility_date = "2024-04-23"

[[d1_databases]]
binding = "DB"
database_name = "example-prod"
database_id = "example-uuid-here"
If I run npx wrangler dev I receive: ✘ [ERROR] Missing entry-point: The entry-point should be specified via the command line (e.g. wrangler dev path/to/script) or the main config field.
Cloudflare Docs
Query D1 from SvelteKit · Cloudflare D1 docs
SvelteKit is a full-stack framework that combines the Svelte front-end framework with Vite for server-side capabilities and rendering. You can query …
Cloudflare Docs
Commands - Wrangler · Cloudflare Workers docs
Create, develop, and deploy your Cloudflare Workers with Wrangler commands.
6 Replies
CaptainNemo
CaptainNemo10mo ago
I also have a D1 + SvelteKit project set up, and for me it runs a local D1 binding when I use vite dev. I also have the binding set in the wrangler TOML like you do. From what I understand, SvelteKit's dev server takes care of finding the binding there.
stibbs
stibbsOP10mo ago
Hmm perhaps I've messed up somewhere else 😦
CaptainNemo
CaptainNemo10mo ago
I bootstrapped my project using this command: npm create cloudflare@latest my-project-name -- --framework=svelte . I followed the instructions here to get my project set up and running without D1: https://developers.cloudflare.com/pages/framework-guides/deploy-a-svelte-site/ Then I added D1 by following steps 3 through 7 on this page, except for step 6 I used npm run dev, which runs vite dev: https://developers.cloudflare.com/d1/get-started/#3-create-a-database
Cloudflare Docs
Deploy a Svelte site · Cloudflare Pages docs
Svelte is an increasingly popular, open-source framework for building user interfaces and web applications. Unlike most frameworks, Svelte is …
Cloudflare Docs
Get started · Cloudflare D1 docs
This guide will instruct you through:
CaptainNemo
CaptainNemo9mo ago
@stibbs did you get it to work?
stibbs
stibbsOP9mo ago
Yes, following those two guides did the trick I don’t know how to mark as solved (on my phone currently and can’t see an option?)
CaptainNemo
CaptainNemo9mo ago
Nice! Glad to hear it.

Did you find this page helpful?