Skip to content

Commit d916cfc

Browse files
authored
feat(dev): support .env.local (#3516)
1 parent 3627c05 commit d916cfc

3 files changed

Lines changed: 9 additions & 5 deletions

File tree

docs/1.guide/97.configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export default defineEventHandler((event) => {
6969

7070
### Local development
7171

72-
Finally, you can update the runtime config using environment variables. You can use a `.env` file in development and use platform variables in production (see below).
72+
Finally, you can update the runtime config using environment variables. You can use a `.env` or `.env.local` file in development and use platform variables in production (see below).
7373

7474
Create an `.env` file in your project root:
7575

docs/2.deploy/20.providers/cloudflare.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,22 +179,22 @@ export default defineEventHandler((event) => {
179179

180180
### Specify Variables in Development Mode
181181

182-
For development, you can use a `.env` file to specify environment variables:
182+
For development, you can use a `.env` or `.env.local` file to specify environment variables:
183183

184184
```ini
185185
NITRO_HELLO_THERE="captain"
186186
SECRET="top-secret"
187187
```
188188

189189
::note
190-
**Note:** Make sure you add `.env` to the `.gitignore` file so that you don't commit it as it can contain sensitive information.
190+
**Note:** Make sure you add `.env` and `.env.local` to the `.gitignore` file so that you don't commit it as it can contain sensitive information.
191191
::
192192

193193
### Specify Variables for local previews
194194

195195
After build, when you try out your project locally with `wrangler dev` or `wrangler pages dev`, in order to have access to environment variables you will need to specify the in a `.dev.vars` file in the root of your project (as presented in the [Pages](https://developers.cloudflare.com/pages/functions/bindings/#interact-with-your-environment-variables-locally) and [Workers](https://developers.cloudflare.com/workers/configuration/environment-variables/#interact-with-environment-variables-locally) documentation).
196196

197-
If you are using a `.env` file while developing, your `.dev.vars` should be identical to it.
197+
If you are using a `.env` or `.env.local` file while developing, your `.dev.vars` should be identical to it.
198198

199199
::note
200200
**Note:** Make sure you add `.dev.vars` to the `.gitignore` file so that you don't commit it as it can contain sensitive information.

src/config/loader.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,18 @@ async function _loadUserConfig(
8585
// prettier-ignore
8686
let preset: string | undefined = (configOverrides.preset as string) || process.env.NITRO_PRESET || process.env.SERVER_PRESET
8787

88+
const _dotenv =
89+
opts.dotenv ??
90+
(configOverrides.dev && { fileName: [".env", ".env.local"] });
8891
const loadedConfig = await (
8992
opts.watch
9093
? watchConfig<NitroConfig & { _meta?: NitroPresetMeta }>
9194
: loadConfig<NitroConfig & { _meta?: NitroPresetMeta }>
9295
)({
9396
name: "nitro",
9497
cwd: configOverrides.rootDir,
95-
dotenv: opts.dotenv ?? configOverrides.dev,
98+
// @ts-expect-error
99+
dotenv: _dotenv,
96100
extend: { extendKey: ["extends", "preset"] },
97101
defaults: NitroDefaults,
98102
jitiOptions: {

0 commit comments

Comments
 (0)