Wrangler "Binding name 'DB' not defined": toml keys, generated types, and the deploy that runs locally
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).
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.