sveltejs / sveltejs/kit

Respect "MODE" to set environment variables from another dotenv file similar to Vite

Open
#16,977 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

awaiting submitter
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the bug

Vite uses a process environment variable of MODE to allow a user to declare another .env file like .env.[MODE] and .env.[MODE].local:

https://vite.dev/guide/env-and-mode#env-files

Svelte conflicts with this by only loading .env and .env.local. Instead, Svelte should also load all environment variables from MODE.

Reproduction
  • Create a new .env.preview file:
MY_SECRET=1234
  • create empty .env and .env.local files

  • Run MODE=preview npm run build

If you try to console.log out that secret, it will be undefined, because SvelteKit is not using .env.preview.

Logs

System Info
System:
    OS: macOS 26.6.2
    CPU: (10) arm64 Apple M5
    Memory: 3.02 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.16.0 - /Users/zjh/.volta/tools/image/node/24.16.0/bin/node
    Yarn: 4.18.0 - /Users/zjh/.volta/tools/image/yarn/4.18.0/bin/yarn
    npm: 11.17.0 - /Users/zjh/.volta/tools/image/npm/11.17.0/bin/npm
  Browsers:
    Brave Browser: 151.1.93.136
    Safari: 26.6.2
  npmPackages:
    @sveltejs/adapter-auto: ^6.0.0 => 6.1.1 
    @sveltejs/adapter-vercel: 5 => 5.10.3 
    @sveltejs/kit: ^2.22.0 => 2.70.3 
    @sveltejs/vite-plugin-svelte: ^6.0.0 => 6.2.4 
    svelte: ^5.5.0 => 5.56.10 
    vite: ^7.0.4 => 7.3.6
Severity

annoyance

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Start from the npm run build entry point and trace how .env and .env.local are loaded, comparing that behavior with Vite's MODE-based env-file rules. Done means MODE=preview loads .env.preview and .env.preview.local so MY_SECRET is available.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, vite
Domain
build-system, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
70/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.