Skip to content
On this page

Chapter 1 - Setting Up The Project

IMPORTANT: This chapter assumes that you already have installed a recent version of Node.js on your computer. If you do not have it yet, you can download it here: https://nodejs.org/en/download/

There are many different ways to create a Svelte app. Here we'll be leveraging TypeScript and therefore will need to setup an project with a build/transpile process that will let us make changes and verify them in real time. You could manually create this project, install all the npm packages required, create each individual file. However, it is just much easier to do this by leveraging vite[1]

Create Project Wizard

To set up the project, use the terminal and execute the following node.js command:

bash
npm init vite@latest

If you do not have already installed the package create-vite@latest[2] it will prompt you to install it. In this case, type y and then enter to proceed:

bash
Need to install the following packages:
  create-vite@latest
Ok to proceed? (y)

The create-vite wizard will start and will ask you the name of the project. The default is vite-project, so change this to my-svelte-project and hit enter:

bash
? Project name: › my-svelte-project

The second step will ask to select a framework. Use the keyboard arrows to scroll down the list and stop at svelte, then hit enter:

bash
? Select a framework: › - Use arrow-keys. Return to submit.
    vanilla
    vue
    react
    preact
    lit
   svelte

The third step will asking which "variant" you want o use. Scroll down to svelte-ts (this is for the version that uses TypeScript) and hit enter:

bash
? Select a variant: › - Use arrow-keys. Return to submit.
    svelte
   svelte-ts

NOTE: in most recent version of Site the options might be slightly different, make sure you select svelte-ts or TypeScript.

This will create a folder called my-svelte-project which is also the name of our project. At the end it should display a message similar to this:

bash
Scaffolding project in /Volumes/projects/my-svelte-project...

Done. Now run:

  cd my-svelte-project
  npm install
  npm run dev

The first command will navigate to the current sub-directory called my-svelte-project, the second one will install all the npm dependencies, the third one will serve the app locally. You'll see a message similar to this displayed:

bash
vite v3.1.0 dev server running at:

  > Local: localhost:3000
  > Network: use `--host` to expose

From the web browser, navigate to the localhost:3000 address and you'll see application home page rendered:

The my-svelte-project has been created with the main view called App.svelte and a component called Counter.svelte.

Chapter 1 Recap

What We Learned

How to create the basic plumbing for a Svelte app using the vite and create-vite@latest

  • How to serve the app locally through the command npm run dev

Observations

  • The app has been created with only preliminary code
  • The app does not do much yet, has only the main App view with some static html in it and a reference to a simple component called Counter

Based on these observations, there are a few improvements that will be making into the next chapter:

Improvements

  • Expand our app functionality by creating our first component

  1. https://vitejs.dev ↩︎

  2. https://www.npmjs.com/package/create-vite/v/2.6.6 ↩︎

This is a sample from the book.