Skip to content

Consistent handling of environment variables #4296

Closed
@Rich-Harris

Description

@Rich-Harris

Describe the problem

SvelteKit doesn't do anything special with environment variables; it delegates to Vite. This is good for the sorts of environment variables Vite deals in — VITE_ prefixed values that are replaced at build time — but less good for other kinds.

Environment variables can be static (known at build time) or dynamic (not known until run time), and private (only accessible to code that runs on the server) or public (accessible to code that runs on the server or the client):

static dynamic
public ✅ import.meta.env.VITE_* ❌ (1)
private ⚠️ can be done with plugins (2) ⚠️ process.env.* (3)
  1. To be fair, I'm not sure what sorts of things would go in this quadrant, though I would be eager to hear examples
  2. You can add something like plugin-replace to selectively replace things at build time, but this is ad hoc, undiscoverable, and involves duplication between .env files and config
  3. process.env is only available in Node-based environments (e.g. adapter-node, adapter-netlify and adapter-vercel — though once the Vercel adapter starts building Edge Functions that will no longer be the case)

It would be great if we had a consistent and idiomatic way of dealing with all of these.

Describe the proposed solution

I don't actually have any proposals to make, I just didn't want #4293 (comment) to get lost. Whatever solution we land on should

  • work with .env/.env.* files
  • enable dead code elimination etc at build time
  • allow autocompletion/auto-imports (e.g. we could generate typings from your .env file)
  • feel consistent across the four quadrants, but with clearly understandable distinctions between them (e.g. $app/env/public vs $app/env/private or whatever)

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions