Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 35 ยท Getting Started

Setting Up a Svelte Project

Create a Svelte 5 project with the sv CLI or Vite, understand the folder layout, run the dev server, and mount your first component.

You need three things to build with Svelte: Node.js, a package manager, and a project created from a template. The official command-line tool is called sv. It can scaffold a full SvelteKit app or a plain Svelte + Vite project, and it can add features later (testing, linting, a database layer) with one command.

This lesson shows the commands as read-only snippets because the site cannot run a build tool. Run them in your own terminal.

Requirements

  • A current LTS release of Node.js, which includes npm.
  • A code editor. The Svelte extension for VS Code gives syntax highlighting, completion and type checking for .svelte files.
  • A browser with developer tools.

Check your install:

Terminal
node --version
npm --version

Create a project

The recommended way is the sv CLI:

Terminal
npx sv create my-app

It asks a few questions: which template (a SvelteKit app, a minimal template, or a library), whether to use TypeScript, and which add-ons to install. Choose the minimal SvelteKit template for now. Then:

Terminal
cd my-app
npm install
npm run dev

The dev server prints a local address, usually http://localhost:5173. Edit a file and the browser updates without a full reload (hot module replacement).

A plain Svelte + Vite app (no SvelteKit)

If you only want a single-page component playground, create a Vite project with the Svelte template:

Terminal
npm create vite@latest my-widget -- --template svelte
cd my-widget
npm install
npm run dev

This gives you index.html, a src/main.js entry and an App.svelte root component. SvelteKit adds routing, server rendering and data loading on top of the same compiler; we cover it in its own chapter.

If you are unsure which to pick, start with SvelteKit. It is the officially recommended way to build apps, and you can still write ordinary components inside it exactly as in this chapter.

Project layout

A plain Vite project looks like this:

TEXT
my-widget/
  index.html          page shell with <div id="app">
  package.json
  vite.config.js      plugin setup (svelte)
  svelte.config.js    compiler options
  src/
    main.js           mounts the root component
    App.svelte        your first component
    lib/              reusable components and helpers
  public/             static files copied as-is

A SvelteKit project adds src/routes/ (pages, each folder is a URL), src/lib/ (shared code, importable as $lib), static/ and src/app.html.

Mounting a component

In plain Svelte 5, src/main.js calls mount from the svelte package:

JavaScript
import { mount } from 'svelte';
import App from './App.svelte';

const app = mount(App, {
  target: document.getElementById('app'),
  props: { name: 'world' }
});

export default app;

mount creates the component and inserts it into the target element. It replaced the old new App({ target }) constructor syntax from Svelte 3 and 4. When you use SvelteKit you never call mount yourself; the framework does.

Your first edit

Replace App.svelte with:

SVELTE
<script>
  let name = $state('world');
</script>

<h1>Hello {name}!</h1>
<input bind:value={name} />

Type in the input and the heading follows. That two-way connection is bind:value, covered in Events and Bindings.

What happens behind the scenes

The @sveltejs/vite-plugin-svelte plugin hands each .svelte file to the compiler. The compiler returns JavaScript (and CSS), and Vite bundles it. In production mode, npm run build creates optimized static files.

Mimic the two-way input (stand-in)

The compiled Svelte version does the same in spirit: an input listener assigns the variable, and a generated update function writes the changed text into the heading.

Useful scripts

Most templates give you these package.json scripts:

Script What it does
npm run dev Starts the dev server with hot reload
npm run build Produces production files
npm run preview Serves the production build locally
npm run check Runs svelte-check for type and accessibility problems

svelte-check is worth running early. It also reports accessibility warnings, such as an image without alt text, which the compiler itself flags too.

Common problems

  • "Unexpected token" in .svelte.js files: runes only work in .svelte files and in modules named .svelte.js or .svelte.ts. A plain .js file does not get the compiler treatment.
  • Port already in use: Vite picks the next free port; read the terminal output for the real address.
  • Old tutorials show export let: that is Svelte 3/4 syntax. In Svelte 5 use $props(); see Props.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command scaffolds a new project with the official Svelte CLI?

    sv is the official CLI. create asks about the template, TypeScript and add-ons.
  2. 2. How do you attach a root component to the page in plain Svelte 5?

    mount from 'svelte' replaced the constructor style used in Svelte 3 and 4.
  3. 3. Where can runes like $state be used outside a .svelte file?

    The compiler only processes runes in .svelte files and in .svelte.js / .svelte.ts modules.
  4. 4. What does npm run check usually do in a Svelte template?

    svelte-check reports TypeScript errors and Svelte warnings, including accessibility hints.

Summary

  • Use npx sv create for SvelteKit apps, or npm create vite@latest with the svelte template for a plain app.
  • npm run dev starts a hot-reloading dev server; npm run build makes production files.
  • mount(App, { target }) attaches a component in plain Svelte 5.
  • Runes live in .svelte and .svelte.js / .svelte.ts files.
  • Run npm run check early to catch type and accessibility problems.

Next: Anatomy of a Svelte Component