You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]>
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
+
<Renderfile="secrets-in-dev"product="workers" />
13
13
14
-
:::caution
15
-
Be sure to add `.dev.vars` to your `.gitignore` so it never gets committed.
16
-
:::
14
+
### Basic setup
17
15
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.
25
17
26
18
<Steps>
27
19
28
-
1. Create a `.dev.vars` file in your project root.
20
+
1. Create a `.dev.vars`/ `.env`file in your project root.
29
21
30
22
2. Add key-value pairs:
31
23
32
-
```ini title=".dev.vars"
24
+
```ini title=".dev.vars/.env"
33
25
API_HOST="localhost:3000"
34
26
DEBUG="true"
35
27
SECRET_TOKEN="my-local-secret-token"
@@ -46,20 +38,23 @@ If you want to manage environment-based configuration that you **want checked in
46
38
47
39
</Steps>
48
40
49
-
## Multiple local environments with `.dev.vars`
41
+
## Multiple local environments
50
42
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.
52
45
53
46
<Steps>
54
47
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`.
56
49
57
50
2. Add key-value pairs:
58
-
```ini title=".dev.vars.staging"
51
+
52
+
```ini title=".dev.vars.staging/.env.staging"
59
53
API_HOST="staging.localhost:3000"
60
54
DEBUG="false"
61
55
SECRET_TOKEN="staging-token"
62
56
```
57
+
63
58
3. Specify the environment when running the `dev` command:
64
59
65
60
**Wrangler**
@@ -70,7 +65,8 @@ To simulate different local environments, you can:
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:
34
34
35
-
```json title=wrangler.json
35
+
```json title=dist/wrangler.json
36
36
{
37
37
"name": "my-worker",
38
38
"compatibility_date": "2025-04-03",
@@ -59,6 +59,10 @@ Running `vite dev` or `vite build` without providing `CLOUDFLARE_ENV` will use t
59
59
As Cloudflare environments are applied at dev and build time, specifying `CLOUDFLARE_ENV` when running `vite preview` or `wrangler deploy` will have no effect.
60
60
:::
61
61
62
+
## Secrets in local development
63
+
64
+
<Renderfile="secrets-in-dev" />
65
+
62
66
## Combining Cloudflare environments and Vite modes
63
67
64
68
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).
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>`.
118
118
119
119
Like other environment variables, secrets are [non-inheritable](/workers/wrangler/configuration/#non-inheritable-keys) and must be defined per environment.
Copy file name to clipboardExpand all lines: src/content/docs/workers/wrangler/system-environment-variables.mdx
+22-1Lines changed: 22 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,45 +16,66 @@ System environment variables are local environment variables that can change Wra
16
16
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.
17
17
18
18
:::note
19
-
20
19
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.
21
20
:::
22
21
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
+
constapiHost=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
+
23
34
## Supported environment variables
24
35
25
36
Wrangler supports the following environment variables:
- 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.
- 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).
- 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.
- 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.
- 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.
- 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`.
Copy file name to clipboardExpand all lines: src/content/partials/workers/secrets-in-dev.mdx
+32-5Lines changed: 32 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,15 +2,42 @@
2
2
{}
3
3
---
4
4
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
+
:::
6
8
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.
8
10
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"
10
18
SECRET_KEY="value"
11
19
API_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
12
20
```
13
21
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"`.
15
42
16
-
Like other environment variables, secrets are [non-inheritable](/workers/wrangler/configuration/#non-inheritable-keys) and must be defined per environment.
0 commit comments