Skip to content

ReferenceError: Cannot access 'private_env' before initialization when importing $env/dynamic/private #14590

@DarthGigi

Description

@DarthGigi

Describe the bug

As of @sveltejs/kit 2.43.7, accessing the env object from $env/dynamic/private seems to cause a ReferenceError: Cannot access 'private_env' before initialization during the build process's analysis phase.

Downgrading to 2.43.6 fixes this.

I believe this is because of #14571

The error occurs in this file:
src/lib/server/db/index.ts:

import { env } from "$env/dynamic/private";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";

const { DATABASE_URL } = env;

const client = postgres(DATABASE_URL);

export const db = drizzle(client, { schema });
export type DB = typeof db;

Error trace:

node:internal/event_target:1101
  process.nextTick(() => { throw err; });
                           ^
ReferenceError [Error]: Cannot access 'private_env' before initialization
    at file:///website/.svelte-kit/output/server/chunks/chunk-2.js:5:26
    at ModuleJob.run (node:internal/modules/esm/module_job:345:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:665:26)
    at async analyse (file:///website/node_modules/.pnpm/@sveltejs+kit@2.43.7_@sveltejs+vite-plugin-svelte@6.2.1_svelte@5.39.8_vite@7.1.8_@types_7425f1e03e58a0dfb323af8897124f46/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:51:19)
    at async MessagePort.<anonymous> (file:////website/node_modules/.pnpm/@sveltejs+kit@2.43.7_@sveltejs+vite-plugin-svelte@6.2.1_svelte@5.39.8_vite@7.1.8_@types_7425f1e03e58a0dfb323af8897124f46/node_modules/@sveltejs/kit/src/utils/fork.js:23:16)

Reproduction

https://github.com/DarthGigi/sveltekit-mvce-env-vars

Please note that the exact error is hard to reproduce as I believe it's a chunking issue as stated above.

Wether you get Error: DATABASE_URL is not set or the private_env error, both are the cause of the same problem.

Logs

System Info

System:
    OS: macOS 26.0.1
    CPU: (14) arm64 Apple M4 Pro
    Memory: 1.33 GB / 48.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.20.0 - /opt/homebrew/opt/node@22/bin/node
    npm: 10.9.3 - /opt/homebrew/opt/node@22/bin/npm
    pnpm: 10.17.1 - ~/Library/pnpm/pnpm
    bun: 1.2.23 - /opt/homebrew/bin/bun
  Browsers:
    Brave Browser: 141.1.83.108
    Chrome: 141.0.7390.55
    Safari: 26.0.1
  npmPackages:
    @sveltejs/adapter-node: ^5.3.3 => 5.3.3 
    @sveltejs/enhanced-img: ^0.8.3 => 0.8.3 
    @sveltejs/kit: ^2.43.7 => 2.43.7 
    @sveltejs/vite-plugin-svelte: ^6.2.1 => 6.2.1 
    svelte: ^5.39.8 => 5.39.8 
    vite: ^7.1.8 => 7.1.8

Severity

blocking an upgrade

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions