Skip to content

Commit a1370e2

Browse files
Document the new .env support for local development
1 parent e4be388 commit a1370e2

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
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/partials/workers/secrets-in-dev.mdx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,26 @@
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+
To define variables to use in local development that you do not want to store in Wrangler config (for example secrets that are defined for production deployment in [Wrangler configuration file](/workers/configuration/environment-variables/#compare-secrets-and-environment-variables)), create either a `.dev.vars` file, or a `.env` file in the root of your project.
6+
You should use either `.dev.vars` or `.env` but not both. If you define a `.dev.vars` file, then values in `.env` files will be ignored for local development.
67

7-
The `.dev.vars` file should be formatted like a `dotenv` file, such as `KEY="VALUE"`:
8+
The `.dev.vars` and `.env` files should be formatted using the `dotenv` syntax, such as `KEY="VALUE"`:
89

9-
```bash title=".dev.vars"
10+
```bash title=".dev.vars / .env"
1011
SECRET_KEY="value"
1112
API_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
1213
```
1314

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.
15+
To set different secrets for each Cloudflare environment, create files named `.dev.vars.<environment-name>` or `.env.<environment-name>`.
16+
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.
1517

16-
Like other environment variables, secrets are [non-inheritable](/workers/wrangler/configuration/#non-inheritable-keys) and must be defined per environment.
18+
- 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.
19+
- 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:
20+
- `.env.<environment-name>.local` (most specific)
21+
- `.env.local`
22+
- `.env.<environment-name>`
23+
- `.env` (least specific)
24+
25+
:::note
26+
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`.
27+
:::

0 commit comments

Comments
 (0)