Β© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Cloudflare DevelopersCD
Cloudflare Developersβ€’10mo agoβ€’
5 replies
thenasim

Having Trouble Getting D1 to Work in Local Development with Drizzle ORM?

Everything works fine when I run drizzle migrate or use Drizzle Studio. Oddly enough, D1 also works perfectly after deploying to a Worker. However, it doesn't function in local development mode.

For context, I'm using React Router v7 along with the Wrangler Vite plugin.

Any ideas or suggestions would be greatly appreciated!

Here is my wrangler.jsonc
{
  "$schema": "node_modules/wrangler/config-schema.json",
  "name": "play-football",
  "compatibility_date": "2025-04-04",
  "compatibility_flags": [
    "nodejs_compat"
  ],
  "main": "./workers/app.ts",
  "observability": {
    "enabled": true
  },
  "vars": {
    "CLOUDFLARE_ACCOUNT_ID": "your_account_id",
    "CLOUDFLARE_DATABASE_ID": "your_database_id",
    "CLOUDFLARE_D1_TOKEN": "your_d1_token"
  },
  "d1_databases": [
    {
      "binding": "DB",
      "database_name": "example-db",
      "database_id": "omitted",
      "migrations_dir": "drizzle"
    }
  ]
}
{
  "$schema": "node_modules/wrangler/config-schema.json",
  "name": "play-football",
  "compatibility_date": "2025-04-04",
  "compatibility_flags": [
    "nodejs_compat"
  ],
  "main": "./workers/app.ts",
  "observability": {
    "enabled": true
  },
  "vars": {
    "CLOUDFLARE_ACCOUNT_ID": "your_account_id",
    "CLOUDFLARE_DATABASE_ID": "your_database_id",
    "CLOUDFLARE_D1_TOKEN": "your_d1_token"
  },
  "d1_databases": [
    {
      "binding": "DB",
      "database_name": "example-db",
      "database_id": "omitted",
      "migrations_dir": "drizzle"
    }
  ]
}

.dev.vars / .env
CLOUDFLARE_ACCOUNT_ID=
CLOUDFLARE_DATABASE_ID=
CLOUDFLARE_D1_TOKEN=
CLOUDFLARE_ACCOUNT_ID=
CLOUDFLARE_DATABASE_ID=
CLOUDFLARE_D1_TOKEN=


Here is my api code.

import { drizzle } from 'drizzle-orm/d1';

export async function getDB(env: Env) {
  const db = drizzle(env.DB);
  return db;
};

export async function loader({ params, context }: Route.LoaderArgs) {
  const db = await getDB(context.cloudflare.env);
  const result = await db.select().from(users).all();

  return Response.json({
    users: result,
  });
}
import { drizzle } from 'drizzle-orm/d1';

export async function getDB(env: Env) {
  const db = drizzle(env.DB);
  return db;
};

export async function loader({ params, context }: Route.LoaderArgs) {
  const db = await getDB(context.cloudflare.env);
  const result = await db.select().from(users).all();

  return Response.json({
    users: result,
  });
}
Screenshot_2025-04-21_093525.png
Cloudflare Developers banner
Cloudflare DevelopersJoin
Welcome to the official Cloudflare Developers server. Here you can ask for help and stay updated with the latest news
85,042Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

How D1 work with Drizzle ORM?
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
12mo ago
SvelteKit (Vite) + D1 + Prisma Local development
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
13mo ago
Cloudflare Worker with Hono + D1 + D1-ORM
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
3y ago
Are queue producers and consumers supposed to work in local development with wrangler?
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
3y ago