dotenv/config in svelte.config.js shadows Vite's .env.[mode] loader, silently dropping .env.production overrides

Author: IvanTheGeekCreated May 25, 2026Updated May 25, 2026

Describe the bug

svelte.config.js (line 2) does:

javascript
import 'dotenv/config';

The dotenv/config side-effect entry reads .env only — not .env.production, not .env.[mode] — and copies its keys into process.env before Vite/SvelteKit start. Vite has its own layered env loader (.env.production → .env.local → .env), but it respects values already present in process.env. So any key set by .env wins, and the corresponding override in .env.production is silently dropped.

This was introduced in #1917 to make process.env.MERMAID_BASE_PATH available to the paths.base line in svelte.config.js. The mechanism works for that one var, but it broke layered-env precedence for every other MERMAID_* key.

To Reproduce

On a fresh develop checkout:

bash
echo "MERMAID_RENDERER_URL='https://example.com/dev-default'" > .env
echo "MERMAID_RENDERER_URL=''" > .env.production
pnpm install
pnpm build
grep -oE 'rendererUrl:"[^"]*"' docs/_app/immutable/chunks/*.js | sort -u

Actual: rendererUrl:"https://example.com/dev-default" (the .env value). Expected: rendererUrl:"" (the .env.production override, matching Vite's documented precedence).

The build doesn't fail and the bundle doesn't warn — values just come from the wrong file. Easy to mistake for "my .env.production didn't take effect."

Proposed fixes (either is fine, one-line change):

  1. Drop import 'dotenv/config'; and read MERMAID_BASE_PATH via loadEnv from vite inside an async config factory (or accept it from the process env directly). Removes the shadowing entirely.
  2. Swap dotenv → dotenv-flow. dotenv-flow/config honors .env.[mode] precedence the same way Vite does, so both loaders agree. Drop-in import replacement.

Happy to send a PR — would you prefer option 1 (drop the import) or option 2 (swap to dotenv-flow)?

Source: mermaid-js/mermaid-live-editor