Overview
Environments allow you to deploy the same Worker code to different configurations for development, staging, and production. Each environment can have different bindings, settings, and routes.Environments enable you to test changes in isolation before promoting to production, using the same codebase with environment-specific configuration.
Environment Types
Cloudflare Workers supports two environment models:Named Environments (Recommended)
Named environments are explicitly defined and grouped under a service name. This is the modern, recommended approach.wrangler.json
Service Environments (Legacy)
The older model where each environment is treated as a separate service. This approach is deprecated but still supported.Environment Configuration
Top-Level Configuration
Settings defined at the top level apply to the default environment:wrangler.json
wrangler deploy
Environment-Specific Overrides
Override any top-level setting per environment:wrangler.json
Configurable Properties
These settings can be overridden per environment:Routes that trigger this Worker (usually set for production only)
Environment variables (non-sensitive configuration)
KV namespace bindings (use separate namespaces per environment)
D1 database bindings
R2 bucket bindings
Durable Object bindings
Service bindings to other Workers
Analytics Engine bindings
Compatibility date for runtime behavior
Compatibility flags for specific features
Resource limits (CPU time, etc.)
Smart Placement configuration
Service Tags
When using named environments, Cloudflare automatically applies service tags to group environments together in the dashboard.How Tags Work
wrangler.json
- Service tag:
service:my-service - Environment tag:
env:productionorenv:staging
- Grouped view in Cloudflare dashboard
- Filtering and searching by service
- Tracking deployments across environments
If no top-level
name is defined, environments won’t be grouped. Always include a service name when using named environments.Environment Patterns
Development, Staging, Production
wrangler.json
Preview Environments
Use separate preview bindings for local development:wrangler.json
wrangler dev, the preview ID is used instead of production.
Feature Branches
Deploy feature branches to separate environments:wrangler.json
Local Development
Development Server
Environment Selection
By default,wrangler dev uses:
- Top-level configuration
preview_idfor bindings when available- Local mocks for KV, D1, etc.
--env, it uses:
- Environment-specific overrides
- Merged with top-level config
- Preview or remote bindings
CI/CD Integration
GitHub Actions Example
.github/workflows/deploy.yml
Environment Variables in CI
Store secrets in CI/CD platform (GitHub Secrets, GitLab CI/CD variables):Migration Guide
From Service to Named Environments
Before (Service Environments):- Add
defined_environmentsarray - Remove
namefrom environment config (inherited from top level) - All environments now grouped under service name
Best Practices
Use Named Environments
Always use
defined_environments for new projects to benefit from dashboard grouping and better organization.Isolate Resources
Use completely separate KV/D1/R2 resources for each environment to prevent data leakage.
Match Compatibility
Keep
compatibility_date the same across environments unless testing runtime upgrades.Test Before Production
Always deploy to staging first, verify functionality, then promote to production.
Troubleshooting
Bindings Not Found
Problem: Worker throws “binding not found” errors Solution: Check that environment-specific bindings are correctly defined:Wrong Environment Deployed
Problem: Changes appear in wrong environment Solution: Always specify--env flag:
Service Tags Missing
Problem: Environments not grouped in dashboard Solution: Ensure top-levelname and defined_environments are set: