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:
{ "name": "my-worker", "compatibility_date": "2025-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" } } }}
name = "my-worker"compatibility_date = "2025-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.
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:
{ "name": "my-worker", "compatibility_date": "2025-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" } } }}
# wrangler.toml
name = "my-worker"compatibility_date = "2025-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" }
Next, provide .env.staging
and .env.production
files:
CLOUDFLARE_ENV=staging
CLOUDFLARE_ENV=production
By default, vite build
uses the 'production' Vite mode.
Vite will therefore load the .env.production
file to get the environment variables that are used in the build.
Since the .env.production
file contains CLOUDFLARE_ENV=production
, the Cloudflare Vite plugin will select the 'production' Cloudflare environment.
The value of MY_VAR
will therefore be 'Production var'
.
If you run vite build --mode staging
then the 'staging' Vite mode will be used and the 'staging' Cloudflare environment will be selected.
The value of MY_VAR
will therefore be 'Staging var'
.
For more information about using .env
files with Vite, see the relevant documentation ↗.
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Products
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark