Svelte Web Framework
Svelte is a cybernetically enhanced web application framework. It operates as a compiler, transforming declarative Svelte components into highly efficient JavaScript that directly manipulates the DOM, rather than relying on a virtual DOM. The current stable version is 5.55.4, with frequent patch releases and regular minor updates, indicating active development and maintenance.
Common errors
-
Error: SvelteKit encountered an error while trying to build your project.
cause This generic error often indicates a problem during the compilation process, such as syntax errors in Svelte components, incorrect build configuration, or missing dependencies.fixCheck the console output for more specific error messages that appear before this line. Ensure all dependencies are installed (`npm install`), resolve any syntax errors in `.svelte` files, and verify your `vite.config.ts` or `svelte.config.js` if you have custom configurations. -
ReferenceError: $ is not defined
cause This error occurs when attempting to use Svelte stores (e.g., `count = $count + 1`) without Svelte's auto-subscription mechanism properly set up, or outside of a compiled Svelte component context.fixEnsure you are using the store with the `$` prefix within a `.svelte` component file, or explicitly subscribe to the store using `.subscribe()` in regular JavaScript/TypeScript files. -
Hydration mismatch
cause This error typically happens when server-side rendered (SSR) HTML doesn't perfectly match the HTML generated by the client-side Svelte application during hydration. Common causes include conditional rendering differences, incorrect handling of empty slots, or async content during SSR.fixInspect the differences in the generated HTML between the server and client. Ensure server-side and client-side logic are consistent, especially for dynamic content. SvelteKit often provides more details in the console regarding the specific mismatch. Check for issues with `{@html ...}` blocks or conditional rendering during initial load. -
TypeError: Cannot assign to read only property 'propName' of object '[object Object]'
cause In Svelte 5, component props are immutable by default. This error occurs if you try to directly modify a prop received by a child component.fixIf a prop needs to be mutable within the child component, create a local `$state` variable and initialize it with the prop's value: `let count = $state(initialCount);` Then, modify `count` instead of `initialCount`. If the parent needs to update the child, use a callback function passed as a prop or leverage two-way binding (`bind:propName`). -
Error: Svelte components must be defined with `export default class ...` or `export function ...`
cause This error indicates that a `.svelte` file is not properly exporting its component, often due to a missing `export default` for a class or function definition, or if a component is missing entirely.fixEnsure your `.svelte` component file has `export default class ComponentName { ... }` or `export function ComponentName(props) { ... }` (for Runes mode). If using a `<script>` block, ensure it's at the top level and correctly defines the component logic.
Warnings
- breaking Svelte 5 introduced 'Runes' as the new default reactivity model, significantly changing how state management (`$state`, `$effect`, `$derived`) and component logic work compared to Svelte 4's assignment-based reactivity. Migrating from Svelte 4 will require refactoring components to use the new API.
- gotcha Svelte is a compiler, not a runtime library that you directly include in the browser. It requires a build step (e.g., using Vite, Rollup, or Webpack) to transform `.svelte` files into executable JavaScript.
- gotcha The `npx sv create` command initializes a SvelteKit project, which is a full-stack framework built on Svelte, not a barebones Svelte application. While recommended, it includes routing, server-side rendering, and other features beyond a simple Svelte SPA.
- gotcha Direct DOM manipulation using `document.querySelector` or similar APIs within Svelte components should generally be avoided. Svelte efficiently manages the DOM based on your component's state.
Install
-
npm install svelte -
yarn add svelte -
pnpm add svelte
Imports
- onMount
import { onMount } from 'svelte'; - tweened
import { tweened } from 'svelte/motion'; - writable
import { writable } from 'svelte/store';
Quickstart
npx sv create my-app cd my-app npm install npm run dev