Svelte Migration Guides
Step-by-step migration from Svelte 4 to Svelte 5 (runes migration) and from SvelteKit 1 to SvelteKit 2, covering breaking changes, automated migration tools, and manual steps.
This guide covers migrating from Svelte 4 to Svelte 5 and from SvelteKit 1 to SvelteKit 2.
Svelte 4 → Svelte 5 Migration
Automated Migration
The recommended approach is to use the sv migrate CLI command, which automates most of the migration:
npx sv migrate svelte-5 Key Breaking Changes
Runes mode: Components can opt into runes mode with
<svelte:options runes={true} />or globally via compiler options. In Svelte 5, new projects default to runes.Event syntax:
on:click→onclick. The compiler provides warnings when legacy syntax is used.Component props:
export let→$props(). The compiler can auto-migrate.Slots → Snippets:
<slot>→{#snippet}+{@render}. Named slots become named snippet props.Reactive declarations:
$:for derived values →$derived().$:for side effects →$effect().Component API:
new Component()→mount()/unmount().<svelte:component>→ conditional rendering or<svelte:element>.Stores: Still supported but runes are preferred for new code.
Per-file Migration Strategy
- Add
<svelte:options runes={true} />to test in runes mode - Run
sv migratefor automated transforms - Manually fix remaining issues (complex reactive patterns, store interop)
- Remove legacy
.sveltefiles from compatibility mode
SvelteKit 1 → SvelteKit 2 Migration
Key Changes
- Load function API: The
loadfunction signature changed —pageandfetchare now grouped under a single argument - Error handling:
throw error()replacesthrow new Error()in some contexts; the@sveltejs/kiterror type is required - Redirects:
throw redirect()replacesthrow new Redirect() - Form actions: Enhanced form handling with
use:enhanceand action results - Hooks API: Updated
handleandhandleErrorsignatures - Path configuration:
paths.baseandpaths.relativebehavior changes
Automated Migration
npx sv migrate sveltekit-2 Manual Steps
- Update
svelte.config.jsfor SvelteKit 2 configuration - Review all load functions for the updated API
- Check error handling for the new
errorandredirectimports - Verify form actions work with the enhanced API
- Test all hooks for the updated signatures
FAQ
- Can Svelte 4 and Svelte 5 components coexist? Yes, through
svelte/legacycompatibility layer. - Do I need to migrate everything at once? No, migration can be incremental with
<svelte:options>per component. - Are stores deprecated? No, but runes are recommended for new code.
See Also
- Svelte 5 Runes — understanding the new reactivity model
- Svelte Legacy Patterns — detailed legacy-to-modern mapping
- SvelteKit Routing — SvelteKit 2 routing changes
- SvelteKit Hooks and Server Runtime — hooks API updates
