-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Add changelog entry for .env support #24067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9f3678e
add changelog entry for .env support
petebacondarwin 10df29b
distinguish cloudflare environments from environment variables
petebacondarwin 3b13b08
Update src/content/changelog/workers/2025-08-01-dot-env-in-local-dev.mdx
petebacondarwin a6012d4
Update src/content/changelog/workers/2025-08-01-dot-env-in-local-dev.mdx
petebacondarwin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/content/changelog/workers/2025-08-01-dot-env-in-local-dev.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| --- | ||
| title: Wrangler and the Cloudflare Vite plugin support `.env` files in local development | ||
| description: Use `.env` files to provide secrets and override environment variables on the `env` object during local development. | ||
| products: | ||
| - workers | ||
| date: 2025-08-01T01:00:00Z | ||
| --- | ||
|
|
||
| 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. | ||
|
|
||
| Previously in local development, if you wanted to provide secrets or environment variables during local development, you had to use `.dev.vars` files. | ||
| This is still supported, but you can now also use `.env` files, which are more familiar to many developers. | ||
|
|
||
| ## Using `.env` files in local development | ||
|
|
||
| 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"`: | ||
|
|
||
| ```bash title=".env" | ||
| TITLE="My Worker" | ||
| API_TOKEN="dev-token" | ||
| ``` | ||
|
|
||
| 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: | ||
|
|
||
| ```javascript | ||
| export default { | ||
| async fetch(request, env) { | ||
| const title = env.TITLE; // "My Worker" | ||
| const apiToken = env.API_TOKEN; // "dev-token" | ||
| const response = await fetch( | ||
| `https://api.example.com/data?token=${apiToken}`, | ||
| ); | ||
| return new Response(`Title: ${title} - ` + (await response.text())); | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| ## Multiple environments with `.env` files | ||
|
|
||
| To set different environment variables for each environment, create files named `.env.<environment-name>`. | ||
|
|
||
| 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. | ||
|
|
||
| For example, if you want to set different environment variables for the `staging` environment, you can create a file named `.env.staging`: | ||
petebacondarwin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ```bash title=".env.staging" | ||
| API_TOKEN="staging-token" | ||
| ``` | ||
|
|
||
| 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`. | ||
|
|
||
| ```javascript | ||
| export default { | ||
| async fetch(request, env) { | ||
| const title = env.TITLE; // "My Worker" (from `.env`) | ||
| const apiToken = env.API_TOKEN; // "staging-token" (from `.env.staging`, overriding the value from `.env`) | ||
| const response = await fetch( | ||
| `https://api.example.com/data?token=${apiToken}`, | ||
| ); | ||
| return new Response(`Title: ${title} - ` + (await response.text())); | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| ## Find out more | ||
|
|
||
| For more information on how to use `.env` files with Wrangler and the Cloudflare Vite plugin, see the following documentation: | ||
|
|
||
| - [Environment variables and secrets](https://pbd-wrangler-dotenv.preview.developers.cloudflare.com/workers/development-testing/environment-variables/) | ||
petebacondarwin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - [Wrangler Documentation](https://developers.cloudflare.com/workers/wrangler) | ||
| - [Cloudflare Vite Plugin Documentation](https://developers.cloudflare.com/workers/wrangler/vite) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.