fix: don't bundle @sveltejs/kit
#13971
@sveltejs/kitClosing issues
Describe the bug
After upgrading to SvelteKit 2.21.5 from SvelteKit 2.21.2, I am starting to get ReferenceError: require is not defined in ES module scope whenever I import a package that uses require in any capacity. This did not happen before SvelteKit 2.21.3.
I thought it might be an issue with specific packages or projects, but it's not. I tested three different projects, all with different dependencies, and the same problem occurred.
Examples of affected packages:
- marked (https://www.npmjs.com/package/marked)
- highlight.js (https://www.npmjs.com/package/highlight.js)
- @google/genai (https://www.npmjs.com/package/@google/genai)
- nodemailer (https://www.npmjs.com/package/nodemailer)
I concluded that the issue is caused by SvelteKit versions >= 2.21.3 because I kept downgrading, and once I went below 2.21.3, things worked as expected.
Reproduction
- Create a new SvelteKit project
- Install one or more of the packages mentioned above
- Import one or more of them in a server module (e.g.
+page.server.ts, code inside a.server.tsfile, or files inside the$lib/serverdirectory) - Build your app
- Run the app and execute any code that would cause the module to be loaded
Logs
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '[REDACTED_PATH]/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at ExportsCache.has (file://[REDACTED_PATH]/build/server/chunks/hooks.server-MojicEX3.js:15490:20)
at Module.patchedRequire (file://[REDACTED_PATH]/build/server/chunks/hooks.server-MojicEX3.js:15618:22)
at Hook._require.Module.require (file://[REDACTED_PATH]/build/server/chunks/hooks.server-MojicEX3.js:15557:28)
at require (node:internal/modules/helpers:135:16)
at Object.<anonymous> ([REDACTED_PATH]/node_modules/highlight.js/lib/index.js:1:12)
at Module._compile (node:internal/modules/cjs/loader:1730:14)
at Object..js (node:internal/modules/cjs/loader:1895:10)
at Module.load (node:internal/modules/cjs/loader:1465:32)
at Function._load (node:internal/modules/cjs/loader:1282:12)
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
System Info
System:
OS: Windows 11 10.0.26100
CPU: (28) x64 Intel(R) Core(TM) i7-14700K
Memory: 35.78 GB / 63.70 GB
Binaries:
Node: 22.16.0 - C:\Program Files\nodejs\node.EXE
npm: 11.4.1 - ~\AppData\Roaming\npm\npm.CMD
bun: 1.2.16 - ~\.bun\bin\bun.EXE
Browsers:
Edge: Chromium (137.0.3296.68)
Internet Explorer: 11.0.26100.1882
npmPackages:
@sveltejs/adapter-node: ^5.2.12 => 5.2.12
@sveltejs/kit: ^2.21.5 => 2.21.5
@sveltejs/vite-plugin-svelte: ^5.1.0 => 5.1.0
svelte: ^5.34.2 => 5.34.2
vite: ^6.3.5 => 6.3.5
Severity
blocking an upgrade
Additional Information
No response
Describe the bug
When I run pnpm build on my local machine I get this:
vite v6.3.5 building SSR bundle for production...
✓ 1524 modules transformed.
✗ Build failed in 3.02s
error during build:
[commonjs--resolver] node_modules/.pnpm/fsevents@2.3.3/node_modules/fsevents/fsevents.node (1:0): Unexpected character '�' (Note that you need plugins to import files that are not JavaScript)
file: /Users/kevin/Workspace/sr/www.soundradix.com/node_modules/.pnpm/fsevents@2.3.3/node_modules/fsevents/fsevents.node:1:0 (/Users/kevin/Workspace/sr/www.soundradix.com/node_modules/.pnpm/chokidar@3.6.0/node_modules/chokidar/index.js)
1: ����@<�
��*...
^
4*
h���/System/Library/Frameworks/CoreFoundation.framework/Versions...
3: ���H��1�H���L���L���H�����L�E�L��H��1�����uH�E�H�[A^A_]��}f.�UH��AWAVSPI��H��I��HH...
at getRollupError (file:///Users/kevin/Workspace/sr/www.soundradix.com/node_modules/.pnpm/rollup@4.44.0/node_modules/rollup/dist/es/shared/parseAst.js:401:41)
at ParseError.initialise (file:///Users/kevin/Workspace/sr/www.soundradix.com/node_modules/.pnpm/rollup@4.44.0/node_modules/rollup/dist/es/shared/node-entry.js:14305:28)
at convertNode (file:///Users/kevin/Workspace/sr/www.soundradix.com/node_modules/.pnpm/rollup@4.44.0/node_modules/rollup/dist/es/shared/node-entry.js:16209:10)
at convertProgram (file:///Users/kevin/Workspace/sr/www.soundradix.com/node_modules/.pnpm/rollup@4.44.0/node_modules/rollup/dist/es/shared/node-entry.js:15452:12)
at Module.setSource (file:///Users/kevin/Workspace/sr/www.soundradix.com/node_modules/.pnpm/rollup@4.44.0/node_modules/rollup/dist/es/shared/node-entry.js:17201:24)
at async ModuleLoader.addModuleSource (file:///Users/kevin/Workspace/sr/www.soundradix.com/node_modules/.pnpm/rollup@4.44.0/node_modules/rollup/dist/es/shared/node-entry.js:21214:13)
ELIFECYCLE Command failed with exit code 1.
It doesn't seem to happen on the server (which runs Ubuntu, local machine is macOS). It also doesn't happen in SvelteKit 2.21.2.
Reproduction
I'm using TailwindCSS, which has chokidar as a dependency. For me it happens as soon as I run pnpm build on macOS.
Logs
System Info
System:
OS: macOS 15.5
CPU: (10) arm64 Apple M1 Max
Memory: 286.86 MB / 32.00 GB
Shell: 4.0.2 - /opt/homebrew/bin/fish
Binaries:
Node: 20.17.0 - ~/Library/pnpm/node
npm: 10.8.2 - ~/Library/pnpm/npm
pnpm: 10.2.1 - ~/Library/pnpm/pnpm
Browsers:
Chrome: 136.0.7103.114
Edge: 137.0.3296.93
Safari: 18.5
npmPackages:
@sveltejs/adapter-node: ^5.2.12 => 5.2.12
@sveltejs/kit: ^2.21.3 => 2.21.3
@sveltejs/vite-plugin-svelte: ^5.1.0 => 5.1.0
svelte: ^5.34.7 => 5.34.7
vite: ^6.3.5 => 6.3.5
Severity
blocking an upgrade
Additional Information
No response
Pull request
closes #13894
We've had a long-running battle with instanceof checks inside the codebase: when you call error(...) or redirect(...) in your code, SvelteKit needs to check that the thrown object is an instance of HttpError or Redirect.
Recently we tried #13843, which attempts to ensure that any of your dependencies that depend on @sveltejs/kit are bundled, since @sveltejs/kit is also bundled, and that means that your app will only have a single copy of SvelteKit (and classes like HttpError) in memory. But that has problems of its own.
This PR goes in the opposite direction, by making it possible to not bundle @sveltejs/kit in the first place. Aside from being a lot simpler, this feels more correct. The one compromise is that we need to expose a @sveltejs/kit/internal package so that classes can be shared between code in @sveltejs/kit and code in $app/... (which is bundled, necessarily, as it relies on virtual modules etc). But because we're controlling how types/index.d.ts is generated, this module is invisible to TypeScript (and therefore won't show up in import autocomplete suggestions etc).
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
- It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- This message body should clearly illustrate what problems it solves.
- Ideally, include a test that fails without this PR but passes with it.
Tests
- Run the tests with
pnpm testand lint the project withpnpm lintandpnpm check
Changesets
- If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.
Edits
- Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.
Info
🦋 Changeset detected
Latest commit: fbf7871
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
| Name | Type |
|---|---|
| @sveltejs/kit | Patch |
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR
There's a type checking issue in the Kit package. Since we import from the Kit package namespace, we're also importing the generated types. This causes the '$service-worker' module to be declared twice. Once in types/index.d.ts and again in src/types/ambient.d.ts.
EDIT: actually there's probably another reason since we've already been importing from the Kit package namespace before this without issues
EDIT 2: ok it seems the issue only happens if we import from the Kit package namespace in a unit test file.
I think you can also fix it (and I think that's preferable) to add a path alias for that namespace import (we already have one for the root for the same reason)
I think you can also fix it (and I think that's preferable) to add a path alias for that namespace import (we already have one for the root for the same reason)
Oh nice. I didn't know you could override packages like that. I've changed it to use the path alias now.
packages/kit/src/exports/internal/index.js, it's likely too big! :(Pro tip: You can prefix GitHub URLs of issues, PRs or discussions with svcl.dev/ to view them on this page! Also try it on a GitHub release ;)