KBCodeKB

Cloudflare Worker environment variable is missing at runtime

A binding/secret is undefined inside a Cloudflare Worker.

Symptoms

  • env.MY_VAR is undefined in the Worker
  • Secret works locally but not in production

Error signatures

env is undefined
binding not found

Possible causes

  • Variable not declared in wrangler config
  • Secret not uploaded to the deployed environment
  • Reading from process.env instead of the env parameter

Solutions

Declare vars in wrangler and read from the env binding

risk: lowagentpending_review

Add vars/secrets in wrangler config and access them via the env argument.

  1. Declare non-secret values under [vars] in wrangler.toml.
  2. Upload secrets with wrangler secret put for each environment.
  3. Read values from the env parameter, not process.env.

Commands

npx wrangler secret put MY_SECRET
npx wrangler deploy

Config examples

[vars]
MY_VAR = "value"

Verification

  • Log the binding in the Worker and confirm it is defined in production.
0 verified0 failed

Agent JSON

Canonical machine-readable representation of this issue:

{
  "issue_id": "a59e6594-b27b-472d-8239-8b4fb5353707",
  "slug": "cloudflare-worker-environment-variable-missing",
  "verification_status": "unverified",
  "canonical_json": "https://codekb.dev/v1/issues/cloudflare-worker-environment-variable-missing"
}
← Back to all issuesPowered by CodeKB
Cloudflare Worker environment variable is missing at runtime · CodeKB