Skip to content

Commit bfe9710

Browse files
Add changelog entry for .env support (#24067)
* add changelog entry for .env support * distinguish cloudflare environments from environment variables * Update src/content/changelog/workers/2025-08-01-dot-env-in-local-dev.mdx * Update src/content/changelog/workers/2025-08-01-dot-env-in-local-dev.mdx
1 parent c815264 commit bfe9710

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: Wrangler and the Cloudflare Vite plugin support `.env` files in local development
3+
description: Use `.env` files to provide secrets and override environment variables on the `env` object during local development.
4+
products:
5+
- workers
6+
date: 2025-08-01T01:00:00Z
7+
---
8+
9+
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.
10+
11+
Previously in local development, if you wanted to provide secrets or environment variables during local development, you had to use `.dev.vars` files.
12+
This is still supported, but you can now also use `.env` files, which are more familiar to many developers.
13+
14+
## Using `.env` files in local development
15+
16+
You can create a `.env` file in your project root to define environment variables that will be used when running `wrangler dev` or `vite dev`. The `.env` file should be formatted like a `dotenv` file, such as `KEY="VALUE"`:
17+
18+
```bash title=".env"
19+
TITLE="My Worker"
20+
API_TOKEN="dev-token"
21+
```
22+
23+
When you run `wrangler dev` or `vite dev`, the environment variables defined in the `.env` file will be available in your Worker code via the `env` object:
24+
25+
```javascript
26+
export default {
27+
async fetch(request, env) {
28+
const title = env.TITLE; // "My Worker"
29+
const apiToken = env.API_TOKEN; // "dev-token"
30+
const response = await fetch(
31+
`https://api.example.com/data?token=${apiToken}`,
32+
);
33+
return new Response(`Title: ${title} - ` + (await response.text()));
34+
},
35+
};
36+
```
37+
38+
## Multiple environments with `.env` files
39+
40+
You may be using [Cloudflare Environments](/workers/wrangler/environments/) to deploy different versions of a Worker with distinct environment variables. For instance, you may have a production and staging environment.
41+
42+
To set different environment variables for each Cloudflare Environment, create files named `.env.<environment-name>`.
43+
44+
When you use `wrangler <command> --env <environment-name>` or `CLOUDFLARE_ENV=<environment-name> vite dev`, the corresponding environment-specific file will also be loaded and merged with the `.env` file.
45+
46+
For example, if you want to set different environment variables for the `staging` environment, you can create a file named `.env.staging`:
47+
48+
```bash title=".env.staging"
49+
API_TOKEN="staging-token"
50+
```
51+
52+
When you run `wrangler dev --env staging` or `CLOUDFLARE_ENV=staging vite dev`, the environment variables from `.env.staging` will be merged onto those from `.env`.
53+
54+
```javascript
55+
export default {
56+
async fetch(request, env) {
57+
const title = env.TITLE; // "My Worker" (from `.env`)
58+
const apiToken = env.API_TOKEN; // "staging-token" (from `.env.staging`, overriding the value from `.env`)
59+
const response = await fetch(
60+
`https://api.example.com/data?token=${apiToken}`,
61+
);
62+
return new Response(`Title: ${title} - ` + (await response.text()));
63+
},
64+
};
65+
```
66+
67+
## Find out more
68+
69+
For more information on how to use `.env` files with Wrangler and the Cloudflare Vite plugin, see the following documentation:
70+
71+
- [Environment variables and secrets](/workers/development-testing/environment-variables)
72+
- [Wrangler Documentation](https://developers.cloudflare.com/workers/wrangler)
73+
- [Cloudflare Vite Plugin Documentation](https://developers.cloudflare.com/workers/wrangler/vite)

0 commit comments

Comments
 (0)