Skip to content
AnswerWell geo Documentation

Research · 9 min read · Answerwell

Worker Environment Variables, Secrets, Local Development, and Deployment Environments

How Cloudflare Workers expose vars and secrets at runtime and build time, configure Wrangler environments, and load .dev.vars and .env locally.

This page summarizes Cloudflare Workers documentation on environment variables and secrets: what they are, how they reach your code at runtime versus during builds, how Wrangler environments split staging and production, and how .dev.vars and .env files work in local development.

Environment variables and secrets: what they are in a Worker

Section titled “Environment variables and secrets: what they are in a Worker”

Environment variables on Cloudflare Workers are a type of binding: plain text strings or JSON values attached to the Worker and exposed on the env object passed into your handler (or on this.env in class-based Workers).

You can add environment variables, which are a type of binding, to attach text strings or JSON values to your Worker. Environment variables are available on the env parameter passed to your Worker’s fetch event handler.

Text strings and JSON values are not encrypted and are useful for storing application configuration.

— Cloudflare, Environment variables

Secrets are also bindings, but they are stored encrypted and are the right place for API keys and tokens. Do not put sensitive values in the vars section of your Wrangler configuration file.

Do not use vars to store sensitive information in your Worker’s Wrangler configuration file. Use secrets instead.

— Cloudflare, Secrets

Kind Where to set (deployed) In code Encrypted
Non-secret config vars in Wrangler, or dashboard Variables and Secrets env.VAR_NAME or import { env } from "cloudflare:workers" No
Secrets wrangler secret put, dashboard, Secrets Store Same env bindings Yes
Build-only values Workers Builds triggers, Pages build settings Available only during the build step Varies (is_secret in Builds API)

vars in Wrangler: how to define non-secret environment variables

Section titled “vars in Wrangler: how to define non-secret environment variables”

Define plaintext and JSON values under vars in your Wrangler configuration file, as described in Wrangler configuration and Environment variables.

{
"vars": {
"API_HOST": "example.com",
"SERVICE_X_DATA": {
"URL": "service-x-api.dev.example",
"MY_ID": 123
}
}
}

Access them in an ES module Worker through env:

export default {
async fetch(request, env) {
return new Response(`API host: ${env.API_HOST}`);
},
};

For code outside the request path (shared clients, module scope), Cloudflare documents importing the global binding:

You can also import env from cloudflare:workers to access environment variables from anywhere in your code, including outside of request handlers.

— Cloudflare, Environment variables

All vars values are strings at the binding layer; parse numbers and booleans in application code, as shown in Agents runtime configuration.

You can also add or edit variables in the dashboard under Workers & Pages → your Worker → Settings → Variables and Secrets, then deploy, per Environment variables.

Wrangler environments: staging, production, and separate Workers

Section titled “Wrangler environments: staging, production, and separate Workers”

Wrangler environments let one project deploy as multiple named Workers with different routes, vars, and bindings. Cloudflare creates a Worker named <top-level-name>-<environment-name> (for example my-worker-staging), as explained in Environments.

When you create an environment, Cloudflare effectively creates a new Worker with the name <top-level-name>-<environment-name>.

— Cloudflare, Environments

Use environments for long-lived staging or development Workers. For per-branch URLs, Cloudflare documents Previews under an environment instead; see Compare workflows.

vars and bindings are not inherited per environment

Section titled “vars and bindings are not inherited per environment”

vars, KV, D1, Durable Objects, and other binding keys are non-inheritable: each environment must declare its own copies. Top-level vars do not flow into [env.production].

Non-inheritable keys are configurable at the top-level, but cannot be inherited by environments and must be specified for each environment.

— Cloudflare, Environments

Example pattern from Environment variables:

{
"vars": { "API_HOST": "api.example.com" },
"env": {
"staging": { "vars": { "API_HOST": "staging.example.com" } },
"production": { "vars": { "API_HOST": "production.example.com" } }
}
}

Deploy or develop against a named environment with --env:

npx wrangler dev --env staging
npx wrangler deploy --env production

CLOUDFLARE_ENV: select an environment without --env on every command

Section titled “CLOUDFLARE_ENV: select an environment without --env on every command”

Wrangler accepts the CLOUDFLARE_ENV process variable for any command that supports --env. The CLI flag wins if both are set.

Set CLOUDFLARE_ENV to specify which environment to use for Wrangler commands … The –env command line argument takes precedence over the CLOUDFLARE_ENV environment variable.

— Cloudflare, Select Wrangler environments using the CLOUDFLARE ENV environment variable

CLOUDFLARE_ENV=production wrangler deploy

With @cloudflare/vite-plugin, pick the active Cloudflare environment at dev or build time via CLOUDFLARE_ENV, not at wrangler deploy --env after a Vite build. Running vite dev or vite build without CLOUDFLARE_ENV uses the top-level Worker config; see Cloudflare Environments and Migrating from wrangler dev.

For Workers Builds, connect one Git repo to each dashboard Worker (my-worker-staging, my-worker-production) and pass --env <name> in deploy and preview build commands, per Advanced setups.

When staging and production sit in different Cloudflare accounts, bind a Wrangler authentication profile per project directory and set account_id in each config so deploys cannot target the wrong account.

Local development: .dev.vars, .env, and per-environment files

Section titled “Local development: .dev.vars, .env, and per-environment files”

Secrets for wrangler dev or vite dev belong in .dev.vars or .env next to your Wrangler configuration file—never committed to git. Tutorial projects often add .dev.vars* to .gitignore; see Deploy an existing project.

Put secrets for use in local development in either a .dev.vars file or a .env file, in the same directory as the Wrangler configuration file.

— Cloudflare, Environment variables and secrets

Pick one file family. If .dev.vars exists, .env files are not merged into the Worker’s env during local development.

Choose to use either .dev.vars or .env but not both. If you define a .dev.vars file, then values in .env files will not be included in the env object during local development.

— Cloudflare, Local development with secrets

Dotenv-style content:

SECRET_KEY="value"
API_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"

Optional secrets.required in Wrangler loads only listed secret names from those files and warns on missing keys; see Secrets.

.env support and merge order (Wrangler and Vite plugin)

Section titled “.env support and merge order (Wrangler and Vite plugin)”

Wrangler and the Cloudflare Vite plugin can load .env for local secrets and overrides on the env object:

Now, you can use .env files to provide secrets and override environment variables on the env object during local development with Wrangler and the Cloudflare Vite plugin.

— Cloudflare, Wrangler and the Cloudflare Vite plugin support .env files in local development

For multiple Wrangler environments, add .env.<environment-name> (or .dev.vars.<environment-name>). With .env, files merge; the most specific path wins:

  1. .env.<environment-name>.local
  2. .env.local
  3. .env.<environment-name>
  4. .env

With .dev.vars.<environment-name>, only that file is loaded when it exists—the generic .dev.vars is skipped, and every secret for that environment must appear in the environment-specific file. Details are in Environment variables and secrets.

Run dev with an environment:

npx wrangler dev --env staging
CLOUDFLARE_ENV=staging npx vite dev

Process-level toggles documented under Bindings (Pages) and Wrangler configuration:

  • CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV=false — disable loading dev vars from .env when you are not using .dev.vars.
  • CLOUDFLARE_INCLUDE_PROCESS_ENV=true — include the host process environment in local dev when no .dev.vars is present (not needed when using secrets.required, which already pulls from process.env).

System .env files for Wrangler CLI credentials (CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID, etc.) are separate from Worker bindings; see System environment variables. During local development, values from project .env can also surface on the Worker’s env object when loaded for dev—not to be confused with the cf CLI’s narrower .env rules in Load credentials from a .env file.

process.env, NODE_ENV, and Node.js compatibility

Section titled “process.env, NODE_ENV, and Node.js compatibility”

Libraries written for Node often read process.env. On Workers, that object was empty unless your code wrote to it. With nodejs_compat and the nodejs_compat_populate_process_env compatibility flag, bindings populate process.env (including secrets).

You can now access environment variables and secrets on process.env when using the nodejs_compat compatibility flag.

— Cloudflare, Access your Worker’s environment variables from process.env

Opt in early by adding nodejs_compat_populate_process_env to compatibility_flags. After 2025-04-01, population becomes the default when nodejs_compat is on and compatibility_date is after 2025-04-01, per the same changelog.

process.env fills lazily on first access. JSON vars that are not string-like may appear as raw JSON strings on process.env; see Environment variables and Node.js compatibility and process. Prefer import { env } from "cloudflare:workers" over replacing the entire process.env object, as warned in process.

process.env.NODE_ENV is special: Wrangler statically replaces it at bundle time ("development" for wrangler dev, "production" for deploy/build), enabling dead-code elimination—see Bundling. Override for a single run with NODE_ENV=staging npx wrangler dev.

For tests, withEnv can temporarily override binding values; see Bindings (env).

Build-time versus runtime environment variables

Section titled “Build-time versus runtime environment variables”

Runtime vars and secrets on the deployed Worker are not the same as variables injected during CI builds.

Workers Builds sets variables per trigger (production versus preview). They exist only for the build process:

Environment variables are set per trigger, meaning you can have different values for production and preview builds.

Note: These are build-time environment variables, available only during the build process. For runtime environment variables, refer to Environment variables.

— Cloudflare, Builds API reference

Configure them in the dashboard or via the Builds API (is_secret: true masks values in logs). Default injected variables for Workers Builds include WORKERS_CI, WORKERS_CI_COMMIT_SHA, and related keys in Build configuration.

Cloudflare Pages injects CF_PAGES, CF_PAGES_COMMIT_SHA, CF_PAGES_BRANCH, CF_PAGES_URL, and CI during site builds; see Build configuration. Pages Functions read runtime variables on context.env and can use dashboard or Wrangler vars; see Bindings. Pages Wrangler config supports only env.preview and env.production overrides, with non-inheritable binding rules described in Wrangler configuration for Pages.

After migrating from Pages to Workers, configure build and runtime variables separately; see Migrate from Pages to Workers.

Cloudflare Pages Functions: local env and CLI bindings

Section titled “Cloudflare Pages Functions: local env and CLI bindings”

For Pages local dev, use the project’s Wrangler file with wrangler pages dev, or pass bindings on the CLI:

npx wrangler pages dev --binding=ENVIRONMENT=development

Pages added .dev.vars support for wrangler pages local development (Wrangler v2.0.16+); see the Pages platform changelog.

  • Storing secrets in vars in wrangler.toml / wrangler.json instead of secrets or dashboard secret type.
  • Assuming top-level vars apply inside [env.staging] without redeclaring them.
  • Overriding one non-inheritable key in an environment (for example vars) without re-listing other bindings such as kv_namespaces, which fails validation on deploy for Pages; see Wrangler configuration for Pages.
  • Keeping both .dev.vars and .env and wondering why .env values never appear.
  • Using .dev.vars.staging but omitting secrets that lived only in .dev.vars (environment-specific file replaces the base file entirely).
  • Deploying a Worker built for one Cloudflare environment to another when using the Vite plugin without matching CLOUDFLARE_ENV between build and deploy.
  • Confusing process.env.NODE_ENV (build-time constant) with runtime env bindings.
  • Deploying the root Worker unintentionally when only production and staging environments are meant to be live; see Workers Best Practices.
  • Committing .dev.vars or API tokens in a project .env; use .dev.vars.example patterns from Deploy buttons and gitignore entries from framework guides.
  1. Run npx wrangler dev and confirm Wrangler lists bindings and reports Using vars defined in .dev.vars when secrets are loaded, as in Using BigQuery with Workers AI.
  2. Deploy with npx wrangler deploy --env <name> (or CLOUDFLARE_ENV=<name> wrangler deploy) and verify the dashboard Worker name matches <name>-<environment> per Environments.
  3. In the Cloudflare dashboard under Workers & Pages → Settings → Variables and Secrets, confirm runtime values match what your code reads from env.
  4. If you use Workers Builds, open build settings and confirm per-trigger variables match Build configuration and are not expected at runtime unless also set as Worker bindings.
  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact sentence you are asking about.