Workers environments: staging and production from one config
Wrangler environments let one config deploy the same Worker to multiple places with different vars, routes, and bindings. The --env flag, name mangling, and the vars inheritance trap.
Wrangler environments
One Wrangler file, multiple deployments. Environments override top-level config per named environment.
{
"name": "my-worker",
"route": "dev.example.com/*",
"vars": { "ENVIRONMENT": "dev" },
"env": {
"staging": {
"vars": { "ENVIRONMENT": "staging" },
"route": "staging.example.com/*"
},
"production": {
"vars": { "ENVIRONMENT": "production" },
"routes": ["example.com/*"]
}
}
}Deploy with wrangler deploy --env staging. The Worker name becomes my-worker-staging (name plus env suffix), and each environment can carry its own routes, KV namespaces, D1 databases, and other bindings.
The traps
varsare non-inheritable. Top-levelvarsdo not flow into environments; every environment must define its own. The config above repeatsENVIRONMENTin each env for exactly this reason.- Custom domains and routes need per-env values. If you deploy to a custom domain or route, each environment needs its own
route/routeskey. A staging deploy without one goes toworkers.dev(or fails, depending on the rest of the config). workers_devper environment. To give staging a*.workers.devsubdomain, setworkers_dev = trueinside that environment's block.- With the Vite plugin, select the environment with
CLOUDFLARE_ENV, not--env; the build flattens to a single config for that environment.
Checklist
- Give each environment its own backing resources (separate KV namespaces, D1 databases); sharing production data with staging is how staging writes end up in prod.
wrangler dev --env stagingfor local work against the staging config.- Name environments
stagingandproduction, notdev2andfinal; future you will thank present you.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.