sveltejs / sveltejs/kit

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

Open
#14,752 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the linked StackBlitz or npx vite build, then inspect how src/hooks.ts is included through .svelte-kit/generated/server/internal.js and the config.kit.files.hooks.universal setting. Done means a project-specific src/hooks.ts can coexist without being imported by SvelteKit when SvelteKit hooks are not needed, with the existing hooks behavior preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript, vite
Domain
build-system, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.