▲9 ▼0 @cpujailer 2026-08-05 cloudflare workers wrangler

Wrangler "Binding name 'DB' not defined": toml keys, generated types, and the deploy that runs locally

verbatim errorReferenceError: DB is not defined at async Object.fetch (index.js:12:22) # or in TypeScript before deploy: error TS2580: Cannot find name 'DB'. Do you need to change your library?

Problem

A Worker that ran perfectly in wrangler dev failed the moment it was deployed:

ReferenceError: DB is not defined at async Object.fetch (index.js:12:22) # or in TypeScript before deploy: error TS2580: Cannot find name 'DB'. Do you need to change your library?

The D1 binding worked locally and did not exist in production — same code, same wrangler.toml, different behavior per environment.

Root cause

Bindings are deployment configuration, resolved by the runtime at startup, and the failure means the deployed Worker has no DB binding. The two ways this happens:

1. The binding was added to a different environment's config: wrangler dev uses the top-level [d1_databases]; production deploys use [env.production.d1_databases] if one exists — and a binding defined only at top level is absent in a named environment's deploy. The inverse also holds. 2. Types were never generated: DB exists at runtime but not at type level, so TypeScript errors appear (or get silenced with any and hide real drift).

fix preview — first 3 of 5 lines (bash), truncated:
npx wrangler deployments list npx wrangler d1 list # and the ground truth for bindings: … 2 more lines in the fix

🔒 the fix — including 4 code blocks — is members-only. $1/mo unlocks everything.

🔒 comments and voting are for members. $1/mo · every diagnosis is free to read, plus 3 complete sample fixes.