Skip to content

Client runtime error Cannot read properties of undefined (reading 'env') after upgrade to @sveltejs/kit@2.37.x (adapter-node, runtime YAML envs) #14379

@TrapStoner

Description

@TrapStoner

Describe the bug

Summary

After upgrading from:

  • @sveltejs/kit@2.27.0@sveltejs/kit@2.37.0
  • Edit: the errors appear on 2.36.3, up until 2.36.2 there are no errors

my production app (built with adapter-node and deployed to GCP, where runtime env vars are passed via YAML) throws a client-side runtime error:

TypeError: Cannot read properties of undefined (reading 'env')

This error only appears in the production deployment that uses runtime YAML-injected environment variables (Cloud Run/GCP) and when running the docker container using docker build -t env-bug -f Dockerfile.dev . & docker run -e PORT=8080 -p 8080:8080 env-bug.
It does not appear in pnpm dev or in pnpm preview locally.


Environment

  • SvelteKit: @sveltejs/kit@2.37.0
  • Svelte: 5.38.6 and 5.38.7 (both reproduce)
  • Vite: 7.0.6 and 7.1.4 (already on Vite 7 before upgrade)
  • Node: 22.14.0
  • Adapter: @sveltejs/adapter-node
  • Runtime: GCP Cloud Run (env vars injected at runtime from YAML)
  • Local: pnpm dev / pnpm preview → no error

What I observed

  • Browser console shows crash pointing to a client chunk. Example snippet from failing chunk:

    const e = globalThis.__sveltekit_1f326rc.env;
    export { e };
  • grep shows all process.env occurrences are in server output and adapter-node chunks:

    .svelte-kit/output/server/chunks/TextUtils.js:635: const qrCodeUrl = process.env.PUBLIC_APP_URL || "https://example.com";
    .svelte-kit/adapter-node/chunks/TextUtils.js:635: const qrCodeUrl = process.env.PUBLIC_APP_URL || "https://example.com";
  • grep -R "process.env" .svelte-kit/output/client -nempty (no process.env usage in client bundle).

  • Tried Vite shim:

    define: { 'process.env': {} }

    did not fix (error persists).

  • Upgraded related deps (Firebase etc.) → no change.

  • Error appeared right after upgrading SvelteKit version.


What I tried (and did not work)

  • define: { 'process.env': {} } → no effect.
  • Ensured no client references to process.env (grep confirms).
  • Verified server chunks use process.env.PUBLIC_* (expected).
  • Tried latest Svelte 5.38.7 → still fails.
  • Upgraded Firebase & other deps → no effect.

Possible cause

  • Maybe a regression in Kit’s runtime bootstrap or adapter-node env serialization between 2.27 → 2.37.

Request

  • Please advise whether this is a bug in Kit’s client runtime or if I’m missing a configuration step for adapter-node + runtime YAML envs introduced after Kit 2.27.0.

Reproduction

Repro Repo

(say that 5 times fast)

Steps to reproduce

  1. pnpm i

  2. Build with pnpm build

  3. GCP deploy OR docker run
    --- GCP ---
    3.1. Create the project in Cloud Run and enable necessary API's (Artifact Registry API, Cloud Run API)
    3.2. Deploy to GCP Cloud Run without baking .env into the image (envs are provided only at runtime via YAML) using pnpm deploy:gcp which needs gcloud cli installed.
    --- DOCKER RUN ---
    3.1. Build the image with docker build -t env-bug -f Dockerfile.dev .
    3.2. Run the container with docker run -e PORT=8080 -p 8080:8080 env-bug

  4. Open the app in the browser.

  5. Observe the console error:

    TypeError: Cannot read properties of undefined (reading 'env')
    

Logs

No errors in server console.
Client console errors: TypeError: Cannot read properties of undefined (reading 'env') at BKRsM9ub.js:1:40 handleError @ app.DFEW0Jxm.js:2 F @ BXmFm1FW.js:11 Fn @ BXmFm1FW.js:11 await in Fn Wn @ BXmFm1FW.js:10 await in Wn (anonymous) @ bg:43 Promise.then (anonymous) @ bg:42 BKRsM9ub.js:1 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'env') at BKRsM9ub.js:1:40 (anonymous) @ BKRsM9ub.js:1 BKRsM9ub.js:1 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'env') at BKRsM9ub.js:1:40

System Info

System:
    OS: Linux 6.16 Fedora Linux 42 (KDE Plasma Desktop Edition)
    CPU: (16) x64 AMD Ryzen 9 7940HS w/ Radeon 780M Graphics
    Memory: 38.46 GB / 54.72 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 22.14.0 - ~/.nvm/versions/node/v22.14.0/bin/node
    npm: 10.9.2 - ~/.nvm/versions/node/v22.14.0/bin/npm
    pnpm: 10.15.1 - ~/.local/share/pnpm/pnpm
  Browsers:
    Brave Browser: 139.1.81.137
  npmPackages:
    @sveltejs/adapter-node: ^5.3.1 => 5.3.1 
    @sveltejs/kit: ^2.27.0 => 2.27.0 
    @sveltejs/vite-plugin-svelte: ^6.1.4 => 6.1.4 
    svelte: ^5.38.7 => 5.38.7 
    vite: ^7.1.4 => 7.1.4

Severity

blocking an upgrade

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions