Cloudflare Workers Environment Variables: Not Inherited

Cloudflare Workers environment variables are bindings on the Worker. Plain values go under vars in the Wrangler configuration file, sensitive ones are added as secrets with wrangler secret put, and local development reads secrets from a .dev.vars or .env file. Code reads all of them from the env object. A named environment such as staging inherits none of them.

The local behaviour described here was observed with Wrangler 4.169.0, the npm latest on 28 November 2026. Nothing was deployed, so every statement about a deployed Worker comes from Cloudflare’s docs and is linked.

Where do you set Cloudflare Workers environment variables?

In two places, and the Worker’s code cannot tell them apart. The environment variables page describes vars as text strings or JSON values that are not encrypted. They live in wrangler.jsonc or wrangler.toml, a file that is normally committed, and Wrangler prints them in its bindings table when it starts. Secrets are the same kind of binding with the value hidden: once set, neither Wrangler nor the dashboard shows it again.

A secret is set per Worker from the command line. The docs say this creates a new version of the Worker and deploys it immediately, and that wrangler versions secret put is the variant for gradual deployments.

npx wrangler secret put API_TOKEN --env staging

This is the configuration the rest of the post refers to: one Worker, two named environments.

{
  "$schema": "./node_modules/wrangler/config-schema.json",
  "name": "orders-api",
  "main": "src/index.ts",
  "compatibility_date": "2026-11-01",
  // Top level: plain `wrangler dev`, and a deploy with no --env.
  "vars": { "API_ORIGIN": "https://dev-api.example.com", "LOG_LEVEL": "debug" },
  "secrets": { "required": ["API_TOKEN"] },
  "env": {
    "staging": {
      // Nothing above is inherited, so every key is repeated.
      "vars": { "API_ORIGIN": "https://staging-api.example.com", "LOG_LEVEL": "debug" },
      "secrets": { "required": ["API_TOKEN"] }
    },
    "production": {
      "vars": { "API_ORIGIN": "https://api.example.com", "LOG_LEVEL": "warn" },
      "secrets": { "required": ["API_TOKEN"] }
    }
  }
}

Why is a variable undefined after —env staging?

Because vars is a non-inheritable key. The environments guide says an environment is effectively a new Worker named <top-level-name>-<environment-name>, here orders-api-staging. It gets the vars written inside env.staging and nothing from the top level.

Wrangler does warn. With LOG_LEVEL removed from env.staging.vars, wrangler dev --env staging printed this, and env.LOG_LEVEL was undefined in the handler:

- "env.staging" environment configuration
  - The following vars exist at the top level, but not on "env.staging.vars".
    This is probably not what you want, since "vars" configuration is not inherited by environments.
    Please add these vars to "env.staging.vars":
    - LOG_LEVEL

The warning did not stop anything. The dev server started, and a dry-run deploy with the same gap exited 0, which is the code a CI job checks. An environment with no vars block at all started with an empty env.

Secrets follow the same rule. A secret put without --env belongs to the top-level Worker, and the docs state that secrets must be defined per environment. The secrets key in the config is non-inheritable too, and Wrangler printed a matching warning when it existed only at the top level.

To see what an environment will receive before it ships, run the deploy as a dry run:

npx wrangler deploy --dry-run --env production

It printed the bindings table for that environment and exited. The table held the two vars and no API_TOKEN, so it will not tell you whether a secret is set. A bare wrangler deploy --dry-run warned that “no target environment was specified” and used the top level. In CI, pass --env every time or set CLOUDFLARE_ENV.

How do you read a variable in Worker code?

Three routes lead to the same values. This Worker uses two of them:

import { env } from "cloudflare:workers";

function debug(message: string): void {
  // No env argument down here, so read the binding from the import.
  if (env.LOG_LEVEL === "debug") console.log(message);
}

export default {
  async fetch(request, env): Promise<Response> {
    const { pathname } = new URL(request.url);
    const upstream = await fetch(new URL(pathname, env.API_ORIGIN), {
      headers: { Authorization: `Bearer ${env.API_TOKEN}` },
    });
    debug(`${pathname} -> ${upstream.status}`);
    return new Response(upstream.body, { status: upstream.status });
  },
} satisfies ExportedHandler<Env>;

The handler’s env argument is the ordinary route. The import from cloudflare:workers is for code with no handler argument in reach: module scope, or a helper several calls down. The bindings reference sets a limit on it. Variables and secrets can be read at the top level, but calls to KV or to another Worker fail there, because Workers do not allow I/O outside a request.

The same page carries a warning about module scope. When a deploy changes only bindings, Cloudflare may reuse isolates that are already running, so an API client built once at the top level from a secret can go on using the old value. Build the client inside the request.

The third route is process.env. It is populated when nodejs_compat is on and the compatibility date is 2025-04-01 or later, and from a compatibility date of 2026-08-04 the Node.js compatibility page says nodejs_compat and nodejs_compat_v2 are on by default. Locally the dates behaved as documented. A Worker dated 2026-08-03 with no flags threw ReferenceError: process is not defined. Dated 2026-08-04, it read process.env.API_ORIGIN. With nodejs_compat and a date of 2025-03-31, process.env existed and was empty.

Values on process.env are strings. A JSON var arrived there as JSON text, while env held the parsed object. And process.env.NODE_ENV is not a variable you set: Wrangler replaces it at build time with "development" under wrangler dev and "production" for a deploy.

Env in that sample comes from npx wrangler types, which writes worker-configuration.d.ts. For the config above it typed LOG_LEVEL as "debug" | "warn", the union across every environment, and API_TOKEN as string.

Where do secrets go in local development?

In a .dev.vars file beside the Wrangler config, one KEY="value" per line, kept out of git. A .env file works as well, but not next to it: with both present, Wrangler logged Using secrets defined in .dev.vars and nothing from .env reached the Worker. Wrangler turns each key into a binding on the local Worker, which is a different job from Node loading a .env file into its own process.

The two families differ per environment. Under --env staging, a .dev.vars.staging file replaced .dev.vars outright, so a key present only in .dev.vars went missing. The .env files merged instead: .env, .env.local, .env.staging and .env.staging.local all loaded, and the most specific file won for each key.

A key in the local file with the same name as a var overrides the var. That is how we point API_ORIGIN at a stub on localhost without editing the config.

secrets.required changes what is loaded. With it set, a .dev.vars key that was neither listed nor a declared var was dropped, and a listed key with no value produced:

Missing required secrets: API_TOKEN. Add them to .dev.vars, .env, or set as environment variables.

The docs say the same list makes wrangler deploy fail when a required secret is not set on the Worker. A staging Worker with no token then becomes a failed deploy instead of a failed request.

What does a deploy do to values set in the dashboard?

The configuration reference says that if you change a variable in the dashboard, Wrangler overrides it on the next deploy, unless keep_vars is true. Secrets are left alone until someone runs wrangler secret delete.

There is one exception, on the secrets page. Deploying a variable with the same name as an existing secret does not fail. The variable replaces the secret, and the secret is deleted. To move a value between the two, the docs recommend creating it under a new name first.

What would we do?

Treat the top level as the local environment and never deploy it. Give every deployed environment its full vars and its own secrets.required, even when the values repeat, and have CI name the environment on every command. Read through the handler’s env by default, import from cloudflare:workers in helpers, and leave process.env to libraries that expect it. Frameworks add their own layer on top, as Astro’s env schema does.

A single Worker with no named environments needs none of the repetition. And a secret shared by many Workers is a case for Secrets Store, Cloudflare’s account-level store, which the docs label beta as of November 2026.

Per-environment configuration, on Workers or on AWS Lambda, is part of the serverless development work we do.

Need this built properly?

Get in touch

Hack the planet!