Development environment: Node.js, Vite, SvelteKit and Git¶
Exercise dev-environment-annex.pdf · PDF
Open the Figma prototype of your app and press play. Type in a text field. You cannot: it is a picture of a text field. Can your prototype save anything? Can it count? Can it fail?
A prototype is a drawing that pretends. From now on you build the real thing: same design, now with a brain. Your first goal is small and non-negotiable: an app of yours, running in the browser and saved in Git.
What you will learn
- Say what each tool does: Node.js, npm, Vite, Svelte, SvelteKit and Git
- Create a project with
npx sv createand run it withnpm run dev - Find your way around the project: which files you edit, which ones you never touch
- Change
+page.svelteand explain why the browser updates without a reload - Make your first commit and explain why
node_modules/is not in it - The trap:
localhost:5173is not a website, and you cannot send it to a friend
The kitchen¶
You wrote the menu in Figma. To cook it you need a kitchen, and a kitchen is several machines. We keep this kitchen all page.
| Tool | What it does | In the kitchen |
|---|---|---|
| Node.js | Runs JavaScript outside the browser, on your PC. Created by Ryan Dahl in 2009. Every other tool here is a JavaScript program that needs it. | The stove: nothing cooks without it |
| npm | Comes with Node.js. Downloads the packages (code written by other people) your project needs into the folder node_modules/. |
The supplier |
| Vite | The dev server: a program that serves your app at localhost:5173 while you work and refreshes it when you save. It also builds the final version. By Evan You, 2020; vite is French for "quick". |
The kitchen line |
| Svelte | A compiler: a program that turns your .svelte files into plain JavaScript. By Rich Harris, 2016. You use Svelte 5 (October 2024). |
The recipe language |
| SvelteKit | The framework around Svelte: the ready-made structure for pages, navigation and data. One folder is one page. | The recipe book: where each dish goes |
And if you burn the dish? Git (Linus Torvalds, 2005) keeps a photo of the project at every commit, so you can always go back. It is the kitchen camera.
flowchart LR
F["<b>.svelte files</b><br>what you write<br>(the recipes)"] -->|"compiled by"| S["<b>Svelte</b><br>(recipe language)"]
S -->|"served by"| V["<b>Vite</b><br>(kitchen line)"]
V -->|"plain HTML, CSS, JavaScript"| B["<b>Browser</b><br>(the customer)"]
N["<b>Node.js</b><br>(the stove)"] -.->|"runs"| S
N -.->|"runs"| V
M["<b>npm</b><br>(the supplier)"] -.->|"downloads"| S
M -.->|"downloads"| V
Careful: Svelte is not a library you load in the browser
jQuery travels to the browser. Svelte does not: it works on your PC, before the browser sees anything. The browser only receives ordinary JavaScript, HTML and CSS.
Two more mix-ups to clear now. Node.js is not a language: the language is JavaScript, and Node.js is the program that runs it outside the browser. And SvelteKit does not replace Svelte: it uses it. Svelte is for components; SvelteKit is for pages, navigation and data.
Connect to your kitchen¶
Your kitchen is not in your house. You work on a virtual machine (VM): a computer of your own, in a data centre, that you reach over the network. Every tool on this page runs there, not on the classroom PC, so wherever this page says "your PC" or "your machine", read "your VM". You only need VS Code, and it talks to the VM through SSH, a secure way of working on a remote computer.
- Go to
vms.iessonferrer.comand press Start on your VM,name-diwith your name. It takes a few minutes. When it is ready, the page shows your password. - In VS Code, install the extension Remote - SSH (
ms-vscode-remote.remote-ssh). -
Press F1, choose
Remote-SSH: Connect to Host...and type your machine, with your name in both places: -
If it asks for the platform, choose Linux. The first time it asks
Are you sure you want to continue connecting: accept. - Type the password from that page. It may ask more than once.
- Open a terminal (
Terminal → New Terminal) and typehostname. It answers with your VM's name.
The bottom-left corner of VS Code now says SSH: name-di.iessonferrer.com, with your name. If the connection times out, your VM is off: start it again. VMs stop by themselves at 22:00, and your work is kept. They only answer from Spain, so turn any VPN off.
Check the stove¶
In that terminal, ask each tool for its version:
SvelteKit 3 needs Node.js 22.17 or newer. If you install Node.js at home, take the LTS (Long Term Support, the version that gets fixes for years). Node.js 26 has been the LTS since 28 October 2026; 24 is an older LTS and works just as well for this course.
Create the project¶
Go to your work folder and run one command:
npx downloads a tool and runs it without installing it. sv is the official Svelte command-line tool. The rest of the line answers the questions sv would otherwise ask you:
| Flag | Means |
|---|---|
notebox |
The name of the new folder |
--template minimal |
An almost empty app |
--no-types |
Plain JavaScript, no type checking |
--no-add-ons |
No extras yet |
--install npm |
Download the packages now, with npm |
The first time, npm asks for permission. The version number you see may be newer:
Press Enter. No other question follows. sv installs the packages and finishes with a What's next? box that shows the two commands of the next step.
Why no TypeScript?
The Svelte docs recommend type checking, and they are right. --no-types is a deliberate choice for a first contact: plain JavaScript and fewer red underlines. TypeScript is one flag away when you want it.
Now start it:
npm run dev starts Vite. The -- passes --open on to Vite, which asks for your browser to open at localhost:5173. You see "Welcome to SvelteKit". If nothing opens, type the address yourself.
Where is this page coming from? The internet? Not the public one. localhost always means this computer, and Vite is running on your VM, not on the PC in front of you. So VS Code does port forwarding: it listens on port 5173 of your PC and carries each request through its SSH connection to the VM.
flowchart LR
B["<b>Browser</b><br>asks localhost:5173<br>(the customer)"] -->|"on your PC"| C["<b>VS Code</b><br>forwards port 5173<br>(the delivery rider)"]
C -->|"SSH, over the network"| V["<b>Vite on your VM</b><br>serves the page<br>(the kitchen line)"]
Close VS Code or lose the network and the page goes.
Now press Ctrl+C in the terminal and reload the browser: the page is gone. Run npm run dev again: it is back. The server lives only while that terminal runs it. Open a second terminal tab for everything else.
Careful: localhost:5173 is not a website
You cannot send that link to a friend. On their machine, localhost means their PC. On yours, it reaches your VM only through your VS Code connection, while npm run dev is running. Publishing an app comes much later: Unit 8.
The project map¶
In the VS Code terminal, type code . to open the folder and accept the recommended extension, Svelte for VS Code. Then look around. Most of what you see is the kitchen's wiring.
| File | What it is | Do I edit it? |
|---|---|---|
package.json |
The project's identity card: its name, its scripts (dev, build) and the packages it needs |
Rarely |
node_modules/ |
The downloaded packages. About 1,700 files that are not yours | Never |
vite.config.js |
Settings for Vite and SvelteKit | Rarely |
src/app.html |
The HTML skeleton every page is poured into | Rarely |
src/routes/+page.svelte |
The home page | Yes |
src/routes/+layout.svelte |
The frame shared by all pages (header, footer) | Yes |
src/lib/ |
Your reusable pieces: components, helpers | Yes |
static/ |
Files served as they are (for example robots.txt) |
Rarely |
.gitignore |
The list of things Git must not store | Rarely |
You will also see package-lock.json (the exact version of every package, written by npm) and a hidden .svelte-kit/ (generated by SvelteKit). You edit neither.
Nearly all your work this term happens inside src/.
Old tutorials will confuse you here
SvelteKit 3 moved its settings from svelte.config.js into vite.config.js, and renamed the import shortcut $lib to #lib. If a tutorial mentions the old names, it is older than your project. If your own generated +layout.svelte says $lib, your sv is an old cached copy: create the project again with npx sv@latest create ....
First edit¶
Replace everything in the home page with this, using your own name:
<h1>NoteBox</h1>
<p>Your notes, in one box.</p>
<p>Made by Ada Lovelace · DAM2</p>
Save with Ctrl+S and look at the browser, not at the editor. It changed by itself, with no reload.
Hot module replacement (HMR): Vite notices the saved file, Svelte recompiles only that file, and the browser swaps it in while the page stays open.
Remember Nielsen's first heuristic from usability heuristics? Visibility of system status. Here the tool gives you, the developer, instant feedback.
Now break it on purpose. Delete the > of </h1> and save. A red overlay appears in the browser with the file, the line and the message Expected token >. Fix it and the overlay goes. Would you rather have this error or a white page?
First commit¶
In the second terminal tab, inside notebox/:
| Command | What it does |
|---|---|
git init |
Turns the folder into a Git repository (a project with a history). Once per project |
git add -A |
Puts every change on the list for the next commit |
git commit -m "..." |
Takes the photo, with a message |
git status |
What changed since the last photo |
git log --oneline |
The list of photos |
If Git answers "Please tell me who you are", it needs a name to sign the photos. Run these once and commit again:
Now check what went in. git status says there is nothing to commit (nothing to commit, working tree clean, or the same in Spanish if your PC is set that way). git log --oneline shows one line.
Here is a puzzle. Your project folder weighs about 55 MB. Your commit holds 14 files, and the whole history takes under 350 KB. Where is the rest?
Open .gitignore. Its first line is node_modules. The packages are not stored, because they do not need to be: package.json says what to download, and npm install downloads it again.
Careful: never commit node_modules/
It adds nothing, because package.json and npm install rebuild it. That is also why a project you clone or unzip does not run straight away: run npm install first, then npm run dev.
Now build the habit. Change your name line, save, and run:
git status shows one modified file, and git log --oneline now shows two lines. Make one commit per finished step, with a message that says what changed.
Careful: Git is not GitHub
Git is the program on your PC that keeps the history. GitHub is a website that stores a copy of that history online. Everything so far is local.
To put your project on GitHub, create an empty repository there and copy its URL. Then:
The commands so far¶
| Command | What it does |
|---|---|
node -v |
Shows the Node.js version |
npx sv create notebox ... |
Creates the project folder from a template and installs its packages |
npm install |
Downloads the packages listed in package.json into node_modules/. Needed after cloning a repository |
npm run dev -- --open |
Starts Vite's dev server and opens the browser |
npm run build |
Builds the final, optimised version. If it fails, your code has an error |
If something fails¶
| You see | Cause | Fix |
|---|---|---|
Connection timed out when connecting |
Your VM is off, or you are on a VPN | Start at vms.iessonferrer.com, wait a few minutes, retry. Turn the VPN off |
node: command not found |
That terminal is on the classroom PC, not on your VM | Check that the bottom-left corner says SSH: ..., and open the terminal from that window |
Error: spawn xdg-open ENOENT |
The VM has no browser for --open. Harmless: the server is running |
Type localhost:5173 in your browser |
The browser cannot reach localhost:5173 |
VS Code is not forwarding the port | In the Ports panel, add 5173, or use the address it shows there |
npx: command not found |
Node.js is not installed, or the terminal was open before you installed it | Install it, then open a new terminal |
npm error engine or "Unsupported engine" |
Your Node.js is too old | Check node -v, then install the current LTS |
| "Missing script: dev" | You are not inside the project | cd notebox |
vite: not found |
The packages were never installed | npm install |
| The port is 5174, not 5173 | Another dev server is still running in another terminal | Close it, or use the port Vite prints |
fatal: not a git repository |
You ran Git outside notebox/, or skipped git init |
cd notebox, or run git init |
Your turn¶
The handout at the top of this page takes you through all of this step by step. Then do it once more, for yourself: create your own app's project the same way, make its home page show the app's name and one sentence, and commit. You will fill that project in during this unit.
Key takeaways¶
- Node.js runs JavaScript on your PC, npm brings the packages, Vite serves and builds, Svelte compiles, SvelteKit organises, Git remembers.
- You work on a VM through VS Code and SSH.
npx sv createmakes the project andnpm run devruns it. - localhost always means this computer. Port forwarding carries
localhost:5173from your PC to your VM, only while you are connected and the server runs. - Nearly all your work happens in
src/, starting withsrc/routes/+page.svelte. You never editnode_modules/. - HMR swaps in your saved change without a reload, and a red overlay points at your mistakes.
- A commit is a photo of the project:
git add -A, thengit commit -m "...". One commit per finished step. node_modules/stays out of Git, becausepackage.jsonandnpm installrebuild it.
Quick check¶
1. Which tool turns your .svelte files into plain JavaScript?
- Svelte
- npm
- Node.js
- Git
Svelte is a compiler: it does its work on your PC, so the browser only receives ordinary JavaScript, HTML and CSS. npm downloads packages, Node.js runs the tools, and Git keeps the history.
2. You send a classmate the link localhost:5173. What do they see?
- Nothing of yours, because on their PC that address means their own machine
- Your app, as long as your dev server is running
- Your app, as long as you are both on the school network
- A copy of your app that Vite has published online
localhost always means this PC. Your app answers at that address only on your PC, because VS Code forwards it to your VM, and only while npm run dev is running there.
3. You change a line in +page.svelte and save. The browser shows the change without reloading. What is doing that?
- Hot module replacement: Vite swaps in the recompiled file
- Git, which restores the latest commit in the browser
- npm, which downloads the page again from the registry
- The browser, which checks the file every second
With HMR, Vite notices the saved file, Svelte recompiles only that file, and the browser swaps it in. This works only while the dev server is running.
4. Why does a new project's commit not include node_modules/?
- It is listed in
.gitignore, andnpm installcan rebuild it frompackage.json - Git cannot store folders with more than a thousand files in them
- It is deleted automatically every time you make a commit
- It is stored on GitHub instead of being stored on your PC
.gitignore tells Git what not to store. The packages are not lost: package.json lists them, so npm install downloads them again whenever they are needed.
5. Which file do you edit to change the home page of your app?
-
src/routes/+page.svelte -
src/app.html -
vite.config.js -
package.json
+page.svelte is the home page. src/app.html is the skeleton every page is poured into, vite.config.js holds settings, and package.json is the project's identity card.
6. Which statement about Git and GitHub is true?
- Git keeps the history on your PC, and GitHub stores an online copy of it
- Git and GitHub are two names for the same program on your PC
- GitHub keeps the history on your PC, and Git stores an online copy
- You need a GitHub account before you can make a commit with Git
Git is a program that works locally: git commit needs no internet and no account. GitHub is a website that can hold a copy of the repository once you push it there.
7. A classmate gives you their project as a zip, without node_modules/. You unzip it and npm run dev fails with vite: not found. What do you do?
- Run
npm installinside the folder, then runnpm run devagain - Ask your classmate to send the
node_modules/folder as well - Run
npx sv createinside the folder to rebuild the project - Run
git initinside the folder, then runnpm run devagain
Vite is one of the packages, and the packages are not there yet. npm install reads package.json and downloads them into node_modules/. Creating the project again would give you an empty app, not your classmate's.
Your app runs, but it is three sad lines. Next you write your first component and it starts to look like NoteBox: Component syntax.