Skip to content

SvelteKit doesn't work without hooks file, can't turn off #14752

@james-pre

Description

@james-pre

Describe the bug

I'm trying to use SvelteKit alongside another library. For that library, I have an src/hooks.ts file used to hook into that other library's lifecycle.

I'm not using SvelteKit's hooks, and yet when I try to build my project SvelteKit imports src/hooks.ts even though it isn't meant for SvelteKit. By making it so SvelteKit can't read it using a quick chmod 000, It looks to be imported by .svelte-kit/generated/server/internal.js.

I think the workaround for this bug says a lot about how not-magical it feels:

export default {
	kit: {
		adapter: node(),
		files: {
			hooks: {
				universal: '/dev/null',
			},
		},
	},
};

SvelteKit doesn't provide any way to say "Hey, I DO NOT want to use hooks in this project", meaning we have to resort to ridiculous workarounds. Futhermore, SvelteKit's planned removal of kit.files.* will result in no way to use SvelteKit alongside other libraries since SvelteKit expects (and in the future will require) an unreasonable invariant.

Reproduction

StackBlitz

At a high level:

  • Create a file for another unrelated library, src/hooks.ts
  • import a named export from node:fs and call it
  • Try to build using npx vite build
  • Error

Logs

$ npx vite build
The `config.kit.files.assets` option is deprecated, and will be removed in a future version
The `config.kit.files.appTemplate` option is deprecated, and will be removed in a future version
Your tsconfig.json should extend the configuration generated by SvelteKit:
{
  "extends": "./.svelte-kit/tsconfig.json"
}
vite v7.1.10 building SSR bundle for production...
✓ 182 modules transformed.
The `config.kit.files.assets` option is deprecated, and will be removed in a future version
The `config.kit.files.appTemplate` option is deprecated, and will be removed in a future version
The `config.kit.files.assets` option is deprecated, and will be removed in a future version
The `config.kit.files.appTemplate` option is deprecated, and will be removed in a future version
vite v7.1.10 building for production...
[plugin vite:resolve] Module "node:fs" has been externalized for browser compatibility, imported by "REDACTED/src/hooks.ts". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.
[plugin vite:resolve] Module "node:fs" has been externalized for browser compatibility, imported by "REDACTED/src/hooks.ts". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details. (x2)
✓ 478 modules transformed.
✗ Build failed in 283ms
✗ Build failed in 788ms
error during build:
[vite-plugin-sveltekit-compile] src/hooks.ts (1:9): "writeFileSync" is not exported by "__vite-browser-external", imported by "src/hooks.ts".
file: REDACTED/src/hooks.ts:1:9

1: import { writeFileSync } from 'node:fs';
            ^

    at getRollupError (file://REDACTED/node_modules/rollup/dist/es/shared/parseAst.js:401:41)
    at error (file://REDACTED/node_modules/rollup/dist/es/shared/parseAst.js:397:42)
    at Module.error (file://REDACTED/node_modules/rollup/dist/es/shared/node-entry.js:16939:16)
    at Module.traceVariable (file://REDACTED/node_modules/rollup/dist/es/shared/node-entry.js:17391:29)
    at ModuleScope.findVariable (file://REDACTED/node_modules/rollup/dist/es/shared/node-entry.js:15061:39)
    at Identifier.bind (file://REDACTED/node_modules/rollup/dist/es/shared/node-entry.js:5413:40)
    at CallExpression.bind (file://REDACTED/node_modules/rollup/dist/es/shared/node-entry.js:2804:23)
    at CallExpression.bind (file://REDACTED/node_modules/rollup/dist/es/shared/node-entry.js:12108:15)
    at ExpressionStatement.bind (file://REDACTED/node_modules/rollup/dist/es/shared/node-entry.js:2804:23)
    at Program.bind (file://REDACTED/node_modules/rollup/dist/es/shared/node-entry.js:2800:28)

System Info

$ npx envinfo --system --binaries --browsers --npmPackages "{svelte,@sveltejs/*,vite}"
Need to install the following packages:
envinfo@7.19.0
Ok to proceed? (y) n

npm error canceled
npm error A complete log of this run can be found in: REDACTED.log

Severity

blocking all usage of SvelteKit

Additional Information

Dependencies: svelte v5.40.2, @sveltejs/kit v2.47.1, @sveltejs/adapter-node v5.4.0
Kernel: Linux
Node v24.9.0

More detailed system and app information is not relevant.

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