|
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 | +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. |
6 | 7 |
|
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"`: |
8 | 9 |
|
9 | | -```bash title=".dev.vars" |
| 10 | +```bash title=".dev.vars / .env" |
10 | 11 | SECRET_KEY="value" |
11 | 12 | API_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" |
12 | 13 | ``` |
13 | 14 |
|
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. |
15 | 17 |
|
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