Skip to content

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

Description

@hammzj

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions