How do you manage environment variables in SvelteKit?
Answer
SvelteKit has a built-in environment variable system with strict client/server separation. $env/static/private: server-only environment variables available at build time. Import: import { DATABASE_URL } from '$env/static/private';. These are replaced with their values at build time and never sent to the browser. $env/static/public: public variables (must be prefixed PUBLIC_ in .env) available everywhere. $env/dynamic/private: server-only variables evaluated at runtime (not build time). Required for serverless deployments where env vars might change per environment. $env/dynamic/public: runtime public variables. SvelteKit prevents private variables from being imported in client code — it throws a build error. Define variables in .env, .env.local, or deployment platform. VITE_ prefix: variables prefixed with VITE_ are exposed to client-side Vite code but use PUBLIC_ prefix for SvelteKit's module system.
Previous
What is the difference between prerendering, SSR, and CSR in SvelteKit?
Next
What are Svelte actions (use:)?
More Svelte / SvelteKit Questions
View all →- Intermediate What are Svelte custom stores and the store contract?
- Intermediate How does SSR (Server-Side Rendering) work in SvelteKit?
- Intermediate What are SvelteKit adapters?
- Intermediate What is the difference between prerendering, SSR, and CSR in SvelteKit?
- Intermediate What are Svelte actions (use:)?