Skip to content

Commit 9f3678e

Browse files
add changelog entry for .env support
1 parent f429ac5 commit 9f3678e

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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, as an alternative to `.dev.vars`, 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+
To set different environment variables for each environment, create files named `.env.<environment-name>`.
41+
42+
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.
43+
44+
For example, if you want to set different environment variables for the `staging` environment, you can create a file named `.env.staging`:
45+
46+
```bash title=".env.staging"
47+
API_TOKEN="staging-token"
48+
```
49+
50+
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`.
51+
52+
```javascript
53+
export default {
54+
async fetch(request, env) {
55+
const title = env.TITLE; // "My Worker" (from `.env`)
56+
const apiToken = env.API_TOKEN; // "staging-token" (from `.env.staging`, overriding the value from `.env`)
57+
const response = await fetch(
58+
`https://api.example.com/data?token=${apiToken}`,
59+
);
60+
return new Response(`Title: ${title} - ` + (await response.text()));
61+
},
62+
};
63+
```
64+
65+
## Find out more
66+
67+
For more information on how to use `.env` files with Wrangler and the Cloudflare Vite plugin, see the following documentation:
68+
69+
- [Environment variables and secrets](https://pbd-wrangler-dotenv.preview.developers.cloudflare.com/workers/development-testing/environment-variables/)
70+
- [Wrangler Documentation](https://developers.cloudflare.com/workers/wrangler)
71+
- [Cloudflare Vite Plugin Documentation](https://developers.cloudflare.com/workers/wrangler/vite)

0 commit comments

Comments
 (0)