sveltejs / sveltejs/kit

Using `$env/dynamic/private` in top level might result in build error

Open
#13,772 4 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

$app/env
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the bug

When reading environment variable in global scope, and the environment variable doesn't exists, it errors out during build time:

import { env } from "$env/dynamic/private";

if (!env.API_URL) {
  throw new Error('API_URL is not defined in the environment variables');
}

export const load = async ({ }) => {
  console.log('Loading page...');
}

output of vite build:

vite v6.3.5 building SSR bundle for production...
✓ 174 modules transformed.

node:internal/event_target:1101
  process.nextTick(() => { throw err; });
                           ^
Error: API_URL is not defined in the environment variables
    at file:///home/filip/tmp/foo/app/.svelte-kit/output/server/entries/pages/_page.server.js:3:9
    at ModuleJob.run (node:internal/modules/esm/module_job:273:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:600:26)
    at async Promise.all (index 2)
    at async analyse (file:///home/filip/tmp/foo/app/node_modules/.pnpm/@sveltejs+kit@2.20.8_@sveltejs+vite-plugin-svelte@5.0.3_svelte@5.28.2_vite@6.3.5__svelte@5.28.2_vite@6.3.5/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:67:16)
    at async MessagePort.<anonymous> (file:///home/filip/tmp/foo/app/node_modules/.pnpm/@sveltejs+kit@2.20.8_@sveltejs+vite-plugin-svelte@5.0.3_svelte@5.28.2_vite@6.3.5__svelte@5.28.2_vite@6.3.5/node_modules/@sveltejs/kit/src/utils/fork.js:23:16)

Node.js v23.9.0
 ELIFECYCLE  Command failed with exit code 1.

If the variable is present, the build ends up fine. Not sure if this is intended behavior, but I would expect the environment variables to be read only when running the app, not during build, when using $env/dynamic

It seems that vite dev works fine.

Reproduction

https://github.com/Gregofi/svelte-env-vars

pnpm install
pnpm run build
Logs

System Info
System:
    OS: Linux 6.14 Arch Linux
    CPU: (16) x64 AMD Ryzen 7 PRO 5850U with Radeon Graphics
    Memory: 22.37 GB / 30.66 GB
    Container: Yes
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 23.9.0 - /usr/bin/node
    Yarn: 1.22.22 - /usr/bin/yarn
    npm: 11.3.0 - /usr/bin/npm
    pnpm: 10.9.0 - /usr/bin/pnpm
    bun: 1.0.26 - ~/.bun/bin/bun
  Browsers:
    Chromium: 135.0.7049.95
  npmPackages:
    @sveltejs/adapter-auto: ^6.0.0 => 6.0.0
    @sveltejs/adapter-node: ^5.2.12 => 5.2.12
    @sveltejs/kit: ^2.20.8 => 2.20.8
    @sveltejs/vite-plugin-svelte: ^5.0.3 => 5.0.3
    svelte: ^5.28.2 => 5.28.2
    vite: ^6.3.5 => 6.3.5
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

Start with the linked reproduction and run pnpm install followed by pnpm run build; compare this with vite dev. Trace the build-time failure through @sveltejs/kit/src/core/postbuild/analyse.js and the generated server entry, then verify that an absent dynamic private environment variable does not cause a build failure while still being available at runtime.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, vite
Domain
backend, build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.