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

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 -n → empty (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

Activity

  1. AntonPavlenkov commented on Sep 7, 2025

    @AntonPavlenkov

    I had same error but issue was with ENV NODE_ENV=$NODE_ENV line in docker. Try to remove that line and this should work. The error is very deceiving.

  2. TrapStoner commented on Oct 22, 2025

    @TrapStoner
    Author

    That doesn't feel like a fix but rather like a workaround. I need NODE_ENV=$NODE_ENV in my dev environment because deploying to GCP my env vars are runtime but i need to set NODE_ENV at build time to development or it will get set to production.

    Edit: sveltekit 2.47.2 STILL has the same issue

  3. PlkMarudny commented on Oct 27, 2025

    @PlkMarudny

    And also 2.48.0, $env/dynamic/public is always undefined. This seems to be a blocker.

  4. itsc-ruben-sprengel commented on Dec 9, 2025

    @itsc-ruben-sprengel

    Hello!

    having the same problem with the mentioned @sveltejs/kit version 2.36.3 and above

    +1 for this issue

  5. jpdom commented on Feb 6, 2026

    @jpdom

    @TrapStoner are you perhaps using vite-plugin-top-level-await ?

  6. jpdom commented on Feb 6, 2026

    @jpdom

    @TrapStoner removing top-level-await plugin seems to fixed this for me.
    Tested using :

    "@sveltejs/kit": "^2.36.2",
    "@sveltejs/adapter-node": "^5.3.2"
    
  7. davidatthepark commented on Feb 16, 2026

    @davidatthepark
  8. added theissue type on Feb 16, 2026
  9. TrapStoner commented on May 11, 2026

    @TrapStoner
    Author

    @TrapStoner are you perhaps using vite-plugin-top-level-await ?

    Nope, not using that dependency.

  10. albertodenia commented on May 20, 2026

    @albertodenia

    In case it helps, I experienced a similar issue with adapter-vercel caused by NODE_ENV not being set to production. Given that redeploying a previously-working build exhibited the issue, something in Vercel's build process or upstream of it seems to have caused it.

  11. teemingc commented on May 20, 2026

    @teemingc
    Member

    Probably fixed by #15852 . I'll close this for now but I'll reopen if folks are still encountering the issue after updating

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions