Skip to content

Commit 7c99879

Browse files
Document the new .env support for local development (#23837)
* Document the new .env support for local development * fixup! Document the new .env support for local development * fixup! Document the new .env support for local development * update development-testing/environment-variables page * fix caution box title * update vite-plugin page * update system environment variables page * clarify env vars that control handling --------- Co-authored-by: kodster28 <[email protected]>
1 parent b9ec94a commit 7c99879

File tree

6 files changed

+82
-28
lines changed

6 files changed

+82
-28
lines changed

src/content/docs/workers/configuration/environment-variables.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ Select the **Secret** type if your environment variable is a [secret](/workers/c
9595

9696
<Render file="env_and_secrets" />
9797

98+
### Local development with secrets
99+
98100
<Render file="secrets-in-dev" />
99101

100102
## Related resources

src/content/docs/workers/development-testing/environment-variables.mdx

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,21 @@ head: []
77
description: Configuring environment variables and secrets for local development
88
---
99

10-
import { PackageManagers, Steps } from "~/components";
10+
import { PackageManagers, Render, Steps } from "~/components";
1111

12-
During local development, you may need to configure **environment variables** (such as API URLs, feature flags) and **secrets** (API tokens, private keys). You can use a `.dev.vars` file in the root of your project to override environment variables for local development, and both [Wrangler](/workers/configuration/environment-variables/#compare-secrets-and-environment-variables) and the [Vite plugin](/workers/vite-plugin/reference/secrets/) will respect this override.
12+
<Render file="secrets-in-dev" product="workers" />
1313

14-
:::caution
15-
Be sure to add `.dev.vars` to your `.gitignore` so it never gets committed.
16-
:::
14+
### Basic setup
1715

18-
### Why use a `.dev.vars` file?
19-
20-
Use `.dev.vars` to set local overrides for environment variables that should not be checked into your repository.
21-
22-
If you want to manage environment-based configuration that you **want checked into your repository** (for example, non-sensitive or shared environment defaults), you can define [environment variables as `[vars]`](/workers/wrangler/environments/#_top) in your Wrangler configuration. Using a `.dev.vars` file is specifically for local-only secrets or configuration that you do not want in version control and only want to inject in local dev sessions.
23-
24-
## Basic setup
16+
Here are steps to set up environment variables for local development using either `.dev.vars` or `.env` files.
2517

2618
<Steps>
2719

28-
1. Create a `.dev.vars` file in your project root.
20+
1. Create a `.dev.vars` / `.env` file in your project root.
2921

3022
2. Add key-value pairs:
3123

32-
```ini title=".dev.vars"
24+
```ini title=".dev.vars/.env"
3325
API_HOST="localhost:3000"
3426
DEBUG="true"
3527
SECRET_TOKEN="my-local-secret-token"
@@ -46,20 +38,23 @@ If you want to manage environment-based configuration that you **want checked in
4638

4739
</Steps>
4840

49-
## Multiple local environments with `.dev.vars`
41+
## Multiple local environments
5042

51-
To simulate different local environments, you can:
43+
To simulate different local environments, you can provide environment-specific files.
44+
For example, you might have a `staging` environment that requires different settings than your development environment.
5245

5346
<Steps>
5447

55-
1. Create a file named `.dev.vars.<environment-name>` . For example, we'll use `.dev.vars.staging`.
48+
1. Create a file named `.dev.vars.<environment-name>`/`.env.<environment-name>`. For example, we can use `.dev.vars.staging`/`.env.staging`.
5649

5750
2. Add key-value pairs:
58-
```ini title=".dev.vars.staging"
51+
52+
```ini title=".dev.vars.staging/.env.staging"
5953
API_HOST="staging.localhost:3000"
6054
DEBUG="false"
6155
SECRET_TOKEN="staging-token"
6256
```
57+
6358
3. Specify the environment when running the `dev` command:
6459

6560
**Wrangler**
@@ -70,7 +65,8 @@ To simulate different local environments, you can:
7065

7166
<PackageManagers type="exec" pkg="vite" args="dev" prefix="CLOUDFLARE_ENV=staging" />
7267

73-
Only the values from `.dev.vars.staging` will be applied instead of `.dev.vars`.
68+
- If using `.dev.vars.staging`, only the values from that file will be applied instead of `.dev.vars`.
69+
- If using `.env.staging`, the values will be merged with `.env` files, with the most specific file taking precedence.
7470

7571
</Steps>
7672

src/content/docs/workers/vite-plugin/reference/cloudflare-environments.mdx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sidebar:
66
description: Using Cloudflare environments with the Vite plugin
77
---
88

9-
import { PackageManagers, WranglerConfig } from "~/components";
9+
import { PackageManagers, Render, WranglerConfig } from "~/components";
1010

1111
A Worker config file may contain configuration for multiple [Cloudflare environments](/workers/wrangler/environments/).
1212
With the Cloudflare Vite plugin, you select a Cloudflare environment at dev or build time by providing the `CLOUDFLARE_ENV` environment variable.
@@ -32,7 +32,7 @@ vars = { MY_VAR = "Production var" }
3232

3333
If you run `CLOUDFLARE_ENV=production vite build` then the output `wrangler.json` file generated by the build will be a flattened configuration for the 'production' Cloudflare environment, as shown in the following example:
3434

35-
```json title=wrangler.json
35+
```json title=dist/wrangler.json
3636
{
3737
"name": "my-worker",
3838
"compatibility_date": "2025-04-03",
@@ -59,6 +59,10 @@ Running `vite dev` or `vite build` without providing `CLOUDFLARE_ENV` will use t
5959
As Cloudflare environments are applied at dev and build time, specifying `CLOUDFLARE_ENV` when running `vite preview` or `wrangler deploy` will have no effect.
6060
:::
6161

62+
## Secrets in local development
63+
64+
<Render file="secrets-in-dev" />
65+
6266
## Combining Cloudflare environments and Vite modes
6367

6468
You may wish to combine the concepts of [Cloudflare environments](/workers/wrangler/environments/) and [Vite modes](https://vite.dev/guide/env-and-mode.html#modes).

src/content/docs/workers/wrangler/environments.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,16 @@ env.staging.service ={ binding = "<BINDING_NAME>", service = "worker-a-staging"
112112

113113
</WranglerConfig>
114114

115-
### Secrets
115+
### Secrets for production
116116

117117
You may assign environment-specific [secrets](/workers/configuration/secrets/) by running the command [`wrangler secret put <KEY> -env`](/workers/wrangler/commands/#put). You can also create `dotenv` type files named `.dev.vars.<environment-name>`.
118118

119119
Like other environment variables, secrets are [non-inheritable](/workers/wrangler/configuration/#non-inheritable-keys) and must be defined per environment.
120120

121+
### Secrets in local development
122+
123+
<Render file="secrets-in-dev" />
124+
121125
---
122126

123127
## Examples

src/content/docs/workers/wrangler/system-environment-variables.mdx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,66 @@ System environment variables are local environment variables that can change Wra
1616
3. Set the values in your shell environment. For example, if you are using Z shell, adding `export CLOUDFLARE_API_TOKEN=...` to your `~/.zshrc` file will set this token as part of your shell configuration.
1717

1818
:::note
19-
2019
To set different system environment variables for each environment, create files named `.env.<environment-name>`. When you use `wrangler <command> --env <environment-name>`, the corresponding environment-specific file will be loaded instead of the `.env` file, so the two files are not merged.
2120
:::
2221

22+
:::note
23+
During local development, the values in `.env` files are also loaded into the `env` object in your Worker, so you can access them in your Worker code.
24+
25+
For example, if you set `API_HOST="localhost:3000"` in your `.env` file, you can access it in your Worker like this:
26+
27+
```js
28+
const apiHost = env.API_HOST;
29+
```
30+
31+
See the [Environment variables and secrets](/workers/development-testing/environment-variables/) page for more information on how to use `.env` files in local development.
32+
:::
33+
2334
## Supported environment variables
2435

2536
Wrangler supports the following environment variables:
2637

2738
- `CLOUDFLARE_ACCOUNT_ID` <Type text="string" /> <MetaInfo text="optional" />
39+
2840
- The [account ID](/fundamentals/account/find-account-and-zone-ids/) for the Workers related account.
2941

3042
- `CLOUDFLARE_API_TOKEN` <Type text="string" /> <MetaInfo text="optional" />
43+
3144
- The [API token](/fundamentals/api/get-started/create-token/) for your Cloudflare account, can be used for authentication for situations like CI/CD, and other automation.
3245

3346
- `CLOUDFLARE_API_KEY` <Type text="string" /> <MetaInfo text="optional" />
47+
3448
- The API key for your Cloudflare account, usually used for older authentication method with `CLOUDFLARE_EMAIL=`.
3549

3650
- `CLOUDFLARE_EMAIL` <Type text="string" /> <MetaInfo text="optional" />
51+
3752
- The email address associated with your Cloudflare account, usually used for older authentication method with `CLOUDFLARE_API_KEY=`.
3853

3954
- `WRANGLER_SEND_METRICS` <Type text="string" /> <MetaInfo text="optional" />
55+
4056
- Options for this are `true` and `false`. Defaults to `true`. Controls whether Wrangler can send anonymous usage data to Cloudflare for this project. You can learn more about this in our [data policy](https://github.com/cloudflare/workers-sdk/tree/main/packages/wrangler/telemetry.md).
4157

4258
- `WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME>`<Type text="string" /> <MetaInfo text="optional" />
59+
4360
- The [local connection string](/hyperdrive/configuration/local-development/) for your database to use in local development with [Hyperdrive](/hyperdrive/). For example, if the binding for your Hyperdrive is named `PROD_DB`, this would be `WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_PROD_DB="postgres://user:[email protected]:5432/testdb"`. Each Hyperdrive is uniquely distinguished by the binding name.
4461

4562
- `CLOUDFLARE_API_BASE_URL` <Type text="string" /> <MetaInfo text="optional" />
63+
4664
- The default value is `"https://api.cloudflare.com/client/v4"`.
4765

4866
- `WRANGLER_LOG` <Type text="string" /> <MetaInfo text="optional" />
67+
4968
- Options for Logging levels are `"none"`, `"error"`, `"warn"`, `"info"`, `"log"` and `"debug"`. Levels are case-insensitive and default to `"log"`. If an invalid level is specified, Wrangler will fallback to the default. Logs can include requests to Cloudflare's API, any usage data being collected, and more verbose error logs.
5069

5170
- `WRANGLER_LOG_PATH` <Type text="string" /> <MetaInfo text="optional" />
71+
5272
- A file or directory path where Wrangler will write debug logs. If the path ends in `.log`, Wrangler will consider this the path to a file where all logs will be written. Otherwise, Wrangler will treat the path as a directory where it will write one or more log files using a timestamp for the filenames.
5373

5474
- `FORCE_COLOR` <Type text="string" /> <MetaInfo text="optional" />
5575
- By setting this to `0`, you can disable Wrangler's colorised output, which makes it easier to read with some terminal setups. For example, `FORCE_COLOR=0`.
5676

5777
* `WRANGLER_HTTPS_KEY_PATH` <Type text="string" /> <MetaInfo text="optional" />
78+
5879
- Path to a custom HTTPS certificate key when running `wrangler dev`, to be used with `WRANGLER_HTTPS_CERT_PATH`.
5980

6081
* `WRANGLER_HTTPS_CERT_PATH` <Type text="string" /> <MetaInfo text="optional" />

src/content/partials/workers/secrets-in-dev.mdx

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,42 @@
22
{}
33
---
44

5-
When developing your Worker or Pages Function, create a `.dev.vars` file in the root of your project to define secrets that will be used when running `wrangler dev` or `wrangler pages dev`, as opposed to using environment variables in the [Wrangler configuration file](/workers/configuration/environment-variables/#compare-secrets-and-environment-variables). This works both in local and remote development modes.
5+
:::caution
6+
Do not use `vars` to store sensitive information in your Worker's Wrangler configuration file. Use secrets instead.
7+
:::
68

7-
The `.dev.vars` file should be formatted like a `dotenv` file, such as `KEY="VALUE"`:
9+
Put secrets for use in local development in either a `.dev.vars` file or a `.env` file, in the root of your project.
810

9-
```bash title=".dev.vars"
11+
:::info
12+
Choose to use either `.dev.vars` or `.env` but not both. If you define a `.dev.vars` file, then values in `.env` files will not be included in the `env` object during local development.
13+
:::
14+
15+
These files should be formatted using the [dotenv](https://hexdocs.pm/dotenvy/dotenv-file-format.html) syntax. For example:
16+
17+
```bash title=".dev.vars / .env"
1018
SECRET_KEY="value"
1119
API_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
1220
```
1321

14-
To set different secrets for each environment, create files named `.dev.vars.<environment-name>`. When you use `wrangler <command> --env <environment-name>`, the corresponding environment-specific file will be loaded instead of the `.dev.vars` file.
22+
:::caution[Do not commit secrets to git]
23+
The `.dev.vars` and `.env` files should not committed to git. Add `.dev.vars*` and `.env*` to your project's `.gitignore` file.
24+
:::
25+
26+
To set different secrets for each Cloudflare environment, create files named `.dev.vars.<environment-name>` or `.env.<environment-name>`.
27+
28+
When you select a Cloudflare environment in your local development, the corresponding environment-specific file will be loaded ahead of the generic `.dev.vars` (or `.env`) file.
29+
30+
- When using `.dev.vars.<environment-name>` files, all secrets must be defined per environment. If `.dev.vars.<environment-name>` exists then only this will be loaded; the `.dev.vars` file will not be loaded.
31+
- In contrast, all matching `.env` files are loaded and the values are merged. For each variable, the value from the most specific file is used, with the following precedence:
32+
- `.env.<environment-name>.local` (most specific)
33+
- `.env.local`
34+
- `.env.<environment-name>`
35+
- `.env` (least specific)
36+
37+
:::note[Controlling `.env` handling]
38+
It is possible to control how `.env` files are loaded in local development by setting environment variables on the process running the tools.
39+
40+
- To disable loading local dev vars from `.env` files without providing a `.dev.vars` file, set the `CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV` environment variable to `"false"`.
41+
- To include every environment variable defined in your system's process environment as a local development variable, ensure there is no `.dev.vars` and then set the `CLOUDFLARE_INCLUDE_PROCESS_ENV` environment variable to `"true"`.
1542

16-
Like other environment variables, secrets are [non-inheritable](/workers/wrangler/configuration/#non-inheritable-keys) and must be defined per environment.
43+
:::

0 commit comments

Comments
 (0)