Build error "Unexpected character '�'" with SvelteKit 2.21.3
#13919
Development PR
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.
Issue
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
Info
I faced the same problem, I need to add these lines into vite.config.ts to bypass this issue.
build: {
rollupOptions: {
external: (id) => {
// External Node.js native modules
if (id === 'fsevents' || id.includes('fsevents')) return true;
if (id === 'chokidar' || id.includes('chokidar')) return true;
// External other problematic modules
if (id.includes('.node')) return true;
return false;
}
}
}
I am using @sveltejs/adapter-cloudflare, it work normally for months, just got this issue today. Weird.
packages:
"@sveltejs/adapter-cloudflare": "^5.1.0",
"@sveltejs/kit": "^2.22.0",
"globals": "^15.15.0",This will probably be fixed by #13894, I'll respond here whenever it's merged so you can check.
@elliott-with-the-longest-name-on-github Could you please point out the root cause? I have read the issue from above but honestly I don't see any relation.
My suspicion is that a CJS module is getting loaded when an ESM one should be instead. Hard to prove. If I get time I'll dig in and see if I can.
My suspicion is that a CJS module is getting loaded when an ESM one should be instead. Hard to prove. If I get time I'll dig in and see if I can.
Thank you so much.
I just tried SvelteKit 2.24.0 and building on macOS seems to work again. (I guess the fix was in 2.22.3)
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 ;)