# Migrating to SvelteKit 3 with sv migrate

- Published: 2026-12-01
- Tags: Why Svelte, Svelte
- URL: https://devtrillo.com/writing/why-svelte-16-migrating-to-sveltekit-3-with-sv-migrate/

> Part 16 of a series on Svelte in 2026. [Part 15](/writing/why-svelte-15-sveltekit-3-whats-new/) went through what SvelteKit 3 changed. This part runs the upgrade tool over a real SvelteKit 2 app and checks its work.

I ran `sv migrate sveltekit-3` on a small notes app, fixed the one config line that stopped the build, and opened the result in a browser. The page rendered. Then I clicked "About" in the nav, and Chromium landed on an error page for a host called `about`.

Nothing had warned me. svelte-check was happy with that file, the build was happy, and the TODO list the tool wrote for me mentioned the same file twice without mentioning the links. That's the short version of this post: the migration does a lot of tedious work correctly, it tells you about most of what it skipped, and you still have to click through your app.

I built the app on SvelteKit 2.70.3, the last 2.x, with the patterns I see in most Kit 2 projects: `$app/stores`, `$env/static/private`, `{base}` in links, a param matcher, shallow routing, a custom alias, a `handleError` hook. Then I committed it and ran sv 1.1.1, which took it to SvelteKit 3.1.0, Svelte 5.57.3, Vite 8.3.5 and TypeScript 6.0.3. I checked the result with svelte-check 4.7.7 and Playwright 1.64.1 against `vite build` plus `vite preview`.

## What the command does

The release post recommends one line, and says the tool will "migrate as much of your codebase as possible, and generate a TODO list for everything else."[^blog]

```sh
npx sv migrate sveltekit-3 --tasks all --confirm
```

The CLI docs list nine tasks.[^cli] sv 1.1.1 ran twelve: the docs' nine plus `imports` (moving types like `Handle` to their new modules), `lib-alias` (`$lib` to `#lib`) and `collect-migration-instructions`, which writes a `MIGRATION_TASKS.md` at the root of the project. Run `npx sv migrate sveltekit-3 --tasks` on its own first; it prints the list your version actually has.

Before you run anything, upgrade to the latest 2.x, as the migration guide says, so you get deprecation warnings first.[^migrate] Don't expect much from them. On 2.70.3 my app printed exactly one, for `csrf.checkOrigin`, and nothing about `$app/stores`, `json()` or the old `error()` signature.

## The part it got right

It touched 15 files and added `src/env.ts`, `src/params.ts` and the task list. Most of the diff is the boring work you'd hate doing by hand, and it was correct.

`svelte.config.js` was deleted and its options moved into the `sveltekit()` plugin, with `prerender.origin` renamed to `paths.origin`. `tsconfig.json` now extends `$app/tsconfig`. Every `$lib` import moved to `#lib`, with `.js` added to the TypeScript ones because Kit 3 wants extensions, and `package.json` got its `imports` block. `$app/environment` became `$app/env`. My one matcher file moved into `src/params.ts` behind `defineParams`. The external redirect got `{ external: true }`. My `$components` alias came across as is, and every build printed `config_option_deprecated_alias` until I swapped its one import for a `#lib` path; that's not on the task list, but it's loud enough not to need it.

The env vars are the best part. It read my imports and wrote this:

```ts title="src/env.ts (generated)"
import { defineEnvVars } from "@sveltejs/kit/env";

// @migration-task Review usage of dynamic environment variables. They fall back to the empty string if not present, which may not be what you want.
export const variables = defineEnvVars({
  PUBLIC_ANALYTICS_ID: { public: true, static: true },
  PUBLIC_CONTACT: { public: true, schema: (input) => input ?? "" },
  DATABASE_URL: { static: true },
});
```

`$env/static/*` imports became `static: true`, the dynamic one got a schema that keeps the old behaviour, and the comment says so. The comment is accurate, and worth more thought than it looks. My About page had `{env.PUBLIC_CONTACT ?? 'nobody'}`, which the tool rewrote to `{PUBLIC_CONTACT ?? 'nobody'}`. With the variable unset, Kit 2 rendered "Contact: nobody". After the migration it rendered "Contact:" and nothing else, because the schema turns `undefined` into `''` before my fallback sees it. Put the fallback in the schema, or make the variable required and let the server refuse to start, which is what part 15 argued for.

## The list it wrote for you

`MIGRATION_TASKS.md` had eight sections, each with a link to the guide and a checklist of files. Six of them were real work: the `csrf.checkOrigin` line it had copied into `vite.config.ts`, a `$app/stores` import in a plain `.ts` file, `error(404, { message, code })` in the old object form, `data-sveltekit-preload-data="off"`, `json()` in an endpoint, and "review `handleError`". One was informational (dev CORS for static assets), and one flagged `Nav.svelte` for mutating `page.url`, which it doesn't do. The file says up front that its findings "are intentionally broad".

The `checkOrigin` line was the only thing that stopped the build: `config_option_removed_check_origin`, and "Failed to load SvelteKit options from Vite config". I deleted it, and `vite build` passed. svelte-check didn't:

```text
src/hooks.server.ts            Property 'status' does not exist on type 'CaughtError<Issue> & …'
src/lib/current-path.ts        Cannot find module '$app/stores' or its corresponding type declarations.
src/routes/photos/+page.svelte Cannot find name 'pushState'.
```

The first two were on the list. The `$app/stores` one matters: the `app-state` task only rewrites `.svelte` files, and my layout read the path from a `derived` store in a `.ts` module. With a passing build, the first request to `/` got a 500 with `app_stores_removed`, and the client threw it on every page. As in part 15, the build lets this one through and only the type check stops it.

The `handleError` item looks like a formality, and it isn't. My Kit 2 hook returned `{ message: 'Something broke' }` for every error, which was fine when it only saw unexpected ones. In Kit 3 it receives all of them,[^migrate] so after the migration `/posts/9` showed a 404 page saying "Something broke" instead of "No such note". svelte-check only noticed the hook because I'd logged `status`, which isn't on the argument anymore. A hook that didn't read it would have type-checked and still eaten every message. The fix is to look at `kind`:

```ts title="src/hooks.server.ts"
export const handleError: HandleServerError = ({ kind, error }) => {
  if (kind !== "unknown") return;
  console.error(error);
  return { message: "Something broke" };
};
```

**Quiz:** After the migration, the build passes and svelte-check reports 0 errors. Your server handleError still returns { message: 'Oops' } for every error, and it never reads status. What does a page that calls error(404, 'Post not found') show?

- Post not found, because handleError only handles unexpected errors
- Oops, with a 404 status
- A 500, because the hook's return type no longer matches

**Answer:** Oops, with a 404 status — The hook's return value replaces the message for every kind of error. In the lab, the missing-note page said 'Something broke' instead of 'No such note' until the hook returned early for anything that wasn't kind 'unknown'.

- Not "Post not found, because handleError only handles unexpected errors": That was Kit 2. In Kit 3, handleError receives expected errors too, with kind set to 'app'.
- Not "A 500, because the hook's return type no longer matches": The return type still matches, so nothing fails. Only reading a property that moved, like status, gives svelte-check something to catch.

## The two it got wrong and didn't mention

The sv docs say the migration "only changes patterns it can identify safely."[^cli] Two of its changes in my app weren't safe, and neither ended up on the list.

The first was shallow routing. My photos page opened a modal with `pushState` straight from a click handler in the markup:

```diff title="src/routes/photos/+page.svelte"
 <script lang="ts">
-	import { pushState } from '$app/navigation';
 	import { page } from '$app/state';
 </script>

 <button onclick={() => pushState('', { photo: 'cat' })}>Open cat</button>
```

The import went, and the call stayed. I tried variations in a second copy of the app. Calls inside `<script>`, `pushState` and `replaceState` both, were rewritten properly, to `goto(url, { shallow: true, state })` and `goto('', { shallow: true, replace: true, state })`. Calls in an attribute in the template were left alone, while the import was removed anyway. svelte-check catches this one ("Cannot find name 'pushState'"), the build doesn't, and in the browser the button threw `pushState is not defined`.

The second was the links, and nothing caught it. `base` is gone from `$app/paths` in Kit 3; you call `resolve()` instead. The tool turned `href={base + '/about'}` into `resolve('about')`, which is right. It turned the much more common string form into this:

```diff title="src/lib/components/Nav.svelte"
-<a href="{base}/about">About</a>
+<a href="{resolve('')}/about">About</a>
```

In Kit 2 with no base path, `base` was `''`, so the href was `/about`. In Kit 3, `resolve('')` isn't an empty string. The server rendered `.//about`, a relative path, because `paths.relative` defaults to true; after hydration the attribute read `//about`. A URL starting with `//` is protocol-relative, so the browser reads `about` as a host name, and that's the error page I opened with. Home and Photos got the same treatment. svelte-check reported 0 errors for that file, and the build passed. The fix is to give `resolve` the whole path:

```svelte
<a href={resolve('/about')}>About</a>
```

I'm reporting this from one app on sv 1.1.1, so it may already be fixed in a newer version. The habit it argues for doesn't depend on the version: after a migration, click every link once.

**What sv migrate left behind.** The Kit 2 code was…

| Kit 2 code | What sv migrate wrote | What caught it | The fix |
| --- | --- | --- | --- |
| csrf: { checkOrigin: true } | Copied into the sveltekit() plugin options as is. | The build and svelte-check, both stopping on config_option_removed_check_origin. Listed in MIGRATION_TASKS.md. | Delete the line. CSRF checks are always on in Kit 3; csrf.trustedOrigins is the allowlist. |
| derived(page, …) in a plain .ts file | Nothing. The app-state task only rewrites .svelte files. | svelte-check ("Cannot find module '$app/stores'"). The build passed; the first request got a 500 with app_stores_removed. Listed in MIGRATION_TASKS.md. | Read page.url.pathname from $app/state where you need it, and delete the store. |
| onclick={() => pushState('', state)} | Removed the pushState import, left the call. | svelte-check ("Cannot find name 'pushState'"). The build passed; clicking threw "pushState is not defined". Not listed. | Import goto and call goto('', { shallow: true, state }). |
| href="{base}/about" | href="{resolve('')}/about" | Nothing. svelte-check 0 errors, build passed. Rendered .//about, then //about after hydration; clicking it left the site. Not listed. | href={resolve('/about')} |
| handleError returning { message: 'Something broke' } | Moved the type import to @sveltejs/kit/hooks. | svelte-check, only because the hook read status. The 404 page for a missing note said "Something broke" instead of "No such note". Listed as "review". | Branch on kind and return early unless it's 'unknown'. |
| error(404, { message, code }) | Nothing. | Nothing; it still works. Listed in MIGRATION_TASKS.md. | error(404, 'No such note', { code }). |
| env.PUBLIC_CONTACT ?? 'nobody' | PUBLIC_CONTACT ?? 'nobody', with a schema of input ?? '' in src/env.ts. | An @migration-task comment in src/env.ts. With the variable unset, the page said "Contact:" instead of "Contact: nobody". | Put the fallback in the schema, or make the variable required. |

## The Next.js codemod blocks the build instead

React has no router to migrate, so the comparison is Next.js. `npx @next/codemod upgrade` runs the codemods for a version and bumps Next, React and React DOM, and when one can't finish a transform it leaves an `@next-codemod-error` comment. The docs are blunt about what that does: "Your build will error until these comments are explicitly removed."[^next] I didn't run it; that's from the docs.

SvelteKit's `@migration-task` comment is softer. My `src/env.ts` built and served with it still in place, and nothing outside the file reminded me. The command's last line does say "1 comment to review. Search for @migration-task", and `MIGRATION_TASKS.md` repeats it, but both are easy to scroll past. I'd rather the build refused, Next.js style, at least for the comments that describe a behaviour change.

## What I'd do on a real app

Commit first and run it on a clean tree, so the diff is the review. sv's docs say to run tasks one at a time and commit between them,[^cli] and that's worth the extra minutes on anything big; one commit for 15 files was already a lot to read. It also reformats code it touches, so expect some noise in the diff.

Then the order that worked for me: `vite build` until it passes, svelte-check until it's clean, `grep -r "@migration-task" src`, work through `MIGRATION_TASKS.md` and delete it, and only then click around the running app. The build caught one leftover, svelte-check three, the task list six, and the links needed a person.

The migration took a few minutes and saved me an afternoon of renames, and I'd run it again. Just don't let the TODO list be the definition of done.

Next: testing Svelte components, with runes.

[^blog]: Svelte blog, [SvelteKit 3 is here](https://svelte.dev/blog/sveltekit-3-is-here).

[^cli]: Svelte CLI docs, [sv migrate](https://svelte.dev/docs/cli/sv-migrate), "sveltekit-3".

[^migrate]: SvelteKit docs, [Migrating to SvelteKit 3](https://svelte.dev/docs/kit/migrating-to-sveltekit-3).

[^next]: Next.js docs, [Codemods](https://nextjs.org/docs/app/guides/upgrading/codemods), "Migrate to async Dynamic APIs".

- **Part 17 — Testing Svelte components**: Vitest and component testing setups that work with runes
