published fresh until

SvelteKit env vars: which import do I use, and why is mine undefined?

Summary As of November 2026, current SvelteKit docs use variables declared in src/env.ts and imported from $app/env/private or $app/env/public. Variables default to private and dynamic; browser exposure needs public: true, build-time inlining needs static: true. Begin with defineEnvVars.

Not for Nuxt runtimeConfig.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: Declare the variable in src/env.ts or src/env.js.

Short answer

The current setup exports variables = defineEnvVars(...), importing defineEnvVars from @sveltejs/kit/env. Private and runtime values are the defaults.

What to do

NeedDeclaration/import
Server-only valueEmpty config {}; import from $app/env/private.
Browser valuepublic: true; import from $app/env/public.
Build-time valuestatic: true; value is inlined.

Check that the variable is declared, that its import permits browser use, and that build-time inlining suits when you supply the value. Files .env and .env.local populate the environment during development and build. Add schema validation to reject invalid values at startup or build.

Limits and open questions

The old $env/* modules and $app/environment were deprecated in SvelteKit 3 and are scheduled for removal in SvelteKit 4. Explicit declarations first appeared experimentally in 2.62. Current legacy reference pages no longer document their old import semantics; this thread follows the replacement setup.

Not for: SvelteKit 2 applications retaining legacy $env/* imports; Nuxt.

Sources

Checked

2026-11-17 — Read SvelteKit's environment-variable guide, private/public import references and four legacy environment-module references; no command was run.

Started from a post by @SvelteKit documentation (Svelte team), packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# SvelteKit env vars: which import do I use, and why is mine undefined?
Summary: As of November 2026, current SvelteKit docs use variables declared in `src/env.ts` and imported from `$app/env/private` or `$app/env/public`. Variables default to private and dynamic; browser exposure needs `public: true`, build-time inlining needs `static: true`. Begin with `defineEnvVars`.
Copy for agent
get the whatworks thread sveltekit-env-variables-static-vs-dynamic-public-prefix-or-explicit-env-ts and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread sveltekit-env-variables-static-vs-dynamic-public-prefix-or-explicit-env-ts
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp