workerd error: R2 binding not found in preview environment
Fixes R2 buckets that work in production but are missing in preview or dev. This skill shows how to declare the r2_buckets binding under the right environment block in wrangler.toml and redeploy the preview. Use it when env.MY_BUCKET throws 'binding not found' only in preview deployments or local dev.
Fix R2 binding not found in preview environment
TL;DR
R2 bucket bindings declared only at the top level of wrangler.toml may not resolve inside preview or named environments. Add the r2_buckets binding under the environment block the preview uses (for example [[env.preview.r2_buckets]]), confirm the bucket exists, and redeploy the preview.
Verbatim error
workerd error: R2 binding not found in preview environmentSteps
- Open wrangler.toml and identify which environment the preview uses (
env.previewforwrangler dev, or your named env). Expected: you know the exact env name. - Add the binding under that environment, e.g.
[[env.preview.r2_buckets]]with the samebindingname andbucket_name. Expected: the binding is present in the env block. - Confirm the bucket exists:
npx wrangler r2 bucket list. Expected: yourbucket_nameappears in the list. - Redeploy (or restart dev) targeting that environment. Expected: boot logs show the binding attached.
- Access
env.MY_BUCKETin a preview request. Expected: no "binding not found" error.
Use this when
- R2 works in production but throws "binding not found" in preview or dev
- You recently added a named environment to wrangler.toml
- Pages preview deployments cannot see the bucket
Not for this skill when
- The bucket is missing in every environment (create the bucket first)
- The
bucket_nameis wrong (fix the name) - The issue is an R2 custom domain or public access setting
Variant phrasings
- r2 binding undefined preview
- env bucket not found workers preview
- r2_buckets env override missing
- R2 binding missing wrangler dev
Why it happens
Preview runs resolve bindings from their environment block. A bucket bound only at the top level is not reliably visible inside named or preview environments, so env.MY_BUCKET resolves to nothing and the runtime reports the binding as not found.
Edge cases
- The
bucket_namemust exist in the same account you deploy to. - Preview often points at the production bucket; use a separate bucket for destructive tests.
wrangler devwithout--envuses the top-level config, which is why the bug only shows in some setups.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_gdXoDyYFciLdxMg84mhqug
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.