-
-
Notifications
You must be signed in to change notification settings - Fork 782
Description
Environment
3.0.1-alpha.2
Reproduction
There's not vite+nitro template so I have to describe this one.
- Create
.env.staging.localwithTEST_VAR=hello - Log
process.env.TEST_VARin any server-side code - Run
vite dev --mode staging - Witness
process.env.TEST_VARis undefined
Describe the bug
When using Nitro as a Vite plugin, mode-specific .env files (e.g. .env.staging.local) are not loaded into process.env for server-side code, even when Vite is started with --mode [mode].
The problem is that in loadOptions(), the dotenv fileName is hardcoded to [".env", ".env.local"].
The Vite plugin has access to configEnv.mode in setupNitroContext() but never forwards it to the dotenv config.
Expected behavior:
Running vite dev --mode [mode] should load .env, .env.local, .env.[mode], and .env.[mode].local into process.env.
Actual behavior:
Only .env and .env.local are loaded. Client-side VITE_-prefixed variables work correctly.
Additional context
What can be done:
The default option should be to evaluate configEnv.mode and add proper file to the dotenv file list.
Another option might be to give user an option to pass 2nd argument to createNitro (currently it's never passed) because opts.dotenv can completely override the dotenv object giving complete flexbility on env files used in userland.