Cloudflare Environments
A Worker config file may contain configuration for multiple Cloudflare environments.
With the Cloudflare Vite plugin, you select a Cloudflare environment at dev or build time by providing the CLOUDFLARE_ENV environment variable.
Consider the following example Worker config file:
{ "$schema": "./node_modules/wrangler/config-schema.json", "name": "my-worker", // Set this to today's date "compatibility_date": "2026-04-03", "main": "./src/index.ts", "vars": { "MY_VAR": "Top-level var" }, "env": { "staging": { "vars": { "MY_VAR": "Staging var" } }, "production": { "vars": { "MY_VAR": "Production var" } } }}"$schema" = "./node_modules/wrangler/config-schema.json"name = "my-worker"# Set this to today's datecompatibility_date = "2026-04-03"main = "./src/index.ts"
[vars]MY_VAR = "Top-level var"
[env.staging.vars]MY_VAR = "Staging var"
[env.production.vars]MY_VAR = "Production var"If you run CLOUDFLARE_ENV=production vite build then the output wrangler.json file generated by the build will be a flattened configuration for the 'production' Cloudflare environment, as shown in the following example:
{ "name": "my-worker", "compatibility_date": "2025-04-03", "main": "index.js", "vars": { "MY_VAR": "Production var" }}Notice that the value of MY_VAR is Production var.
This flattened configuration combines top-level only, inheritable, and non-inheritable keys.
Cloudflare environments can also be used in development.
For example, you could run CLOUDFLARE_ENV=development vite dev.
It is common to use the default top-level environment as the development environment and then add additional environments as necessary.
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.
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.
These files should be formatted using the dotenv ↗ syntax. For example:
SECRET_KEY="value"API_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"To set different secrets for each Cloudflare environment, create files named .dev.vars.<environment-name> or .env.<environment-name>.
When you select a Cloudflare environment in your local development, the corresponding environment-specific file will be loaded ahead of the generic .dev.vars (or .env) file.
- When using
.dev.vars.<environment-name>files, all secrets must be defined per environment. If.dev.vars.<environment-name>exists then only this will be loaded; the.dev.varsfile will not be loaded. - In contrast, all matching
.envfiles are loaded and the values are merged. For each variable, the value from the most specific file is used, with the following precedence:.env.<environment-name>.local(most specific).env.local.env.<environment-name>.env(least specific)
You may wish to combine the concepts of Cloudflare environments and Vite modes ↗. With this approach, the Vite mode can be used to select the Cloudflare environment and a single method can be used to determine environment specific configuration and code. Consider again the previous example:
{ "$schema": "./node_modules/wrangler/config-schema.json", "name": "my-worker", // Set this to today's date "compatibility_date": "2026-04-03", "main": "./src/index.ts", "vars": { "MY_VAR": "Top-level var" }, "env": { "staging