Skip to content

Sveltekit 2 + adapter-node 2 unable to disable prerender, '$env/dynamic/private' used in hooks.server.ts #11341

@jonshipman

Description

@jonshipman

Describe the bug

Migration to SvelteKit 2, updated adapter-node to v2. Modified src/routes/+layout.server.ts to export const prerender = false; and added config.kit.prerender.crawl = false and config.kit.prerender.entries = [] to the svelte config.

Importing a dynamic $env/dynamic/private in hooks.server.ts because the variable is optional (typically use dynamic variables for items not strictly sensitive, e.g. username, hosts, ids, where the default value is committed to the repo).

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

export const DBHOST = env.DBHOST || 'http://localhost:3067';

Interim fix: using process.env will work.

Reproduction

https://stackblitz.com/edit/envdynamicprivate?file=src%2Fhooks.server.js

Run npm run build to see the error message.

Logs

Error: Cannot read values from $env/dynamic/private while prerendering (attempted to read env.test). Use $env/static/private instead
    at _0x2e26db._evaluate (https://sveltejskittemplatedefaultvaof-yhpd.w-corp.staticblitz.com/blitz.a2aabdd9.js:352:376700)
    at async ModuleJob.run (https://sveltejskittemplatedefaultvaof-yhpd.w-corp.staticblitz.com/blitz.a2aabdd9.js:181:2372)

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.10.5 - /usr/local/bin/pnpm
  npmPackages:
    @sveltejs/adapter-auto: ^3.0.0 => 3.0.0 
    @sveltejs/adapter-node: ^2.0.0 => 2.0.0 
    @sveltejs/kit: ^2.0.0 => 2.0.0 
    @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.0.1 
    svelte: ^4.2.7 => 4.2.8 
    vite: ^5.0.3 => 5.0.9

Severity

annoyance

Additional Information

As stated, we can use process.env as a stop gap until this is resolved. My assumption is that the check for $env/dynamic/private needs to run after the declaration of prerender false. hooks.server.js loads before prerender value is established.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions