A minimal reproduction of an issue introduced in sveltekit 2.20 https://github.com/sveltejs/kit/issues/13630
The following has already been configured in current repo:
Dependencies
Ensure @sveltejs/kit is at a version later than 2.19.2. It can be 2.20.0, 2.20.1 or 2.20.2.
Ensure vite-plugin-top-level-await is a dependency and is correctly setup in vite.config.ts.
Hooks
hooks.server.ts file. A minimal configuration is enough for reproduction.Emulate a Production Environment
Run the following command. It will compile the sveltekit app and run a minimum production server.
make prod
Expected Error
The production server will end up with the following error:
file:///build/handler.js:1174
const server = new Server(manifest);
^
TypeError: Server is not a constructor
at file:///build/handler.js:1174:16
at ModuleJob.run (node:internal/modules/esm/module_job:271:25)
at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:578:26)
at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:116:5)
The following command produced a development server. It has no issues.
make dev
2.19.2Only modify @sveltejs/kit in app/package.json from 2.20.2 to 2.19.2. Keep everything else unmodified.
Rerun make prod. No issue.
vite-plugin-top-level-awaitOnly remove vite-plugin-top-level-await from app/package.json and app/vite.config.ts. Keep everything else unmodified.
Rerun make prod. No issue.
hooks.server.tsOnly delete file app/src/hooks.server.ts. Keep everything else unmodified.
Rerun make prod. No issue.
When vite-plugin-top-level-await is used and hooks.server.ts exists, SvelteKit 2.20.0 introduced an issue to NodeJS runtime.
vite-plugin-top-level-await and hooks.server.ts are absolutely required, use SvelteKit 2.19.2 instead.vite-plugin-top-level-await can then be avoided by setting build.target as esnext in vite.config.ts.