CHAPTER 03
Beginner
Svelte Project Structure
Updated: May 18, 2026
5 min read
# CHAPTER 3
Svelte Project Structure
1. Chapter Introduction
Understanding the project structure is the foundation of productive Svelte development. Every file has a purpose, and knowing where code belongs keeps projects maintainable as they grow. In this chapter, we explore every file and folder in a Svelte/Vite project and establish best practices for organizing large applications.2. Learning Objectives
- Understand the role of every file in a new Svelte project.
- Know where to place components, utilities, and assets.
-
Understand
vite.config.jsandpackage.json.
- Establish a scalable folder structure for real applications.
3. Full Project Structure
text
4. index.html — The SPA Shell
Unlike traditional websites, Svelte has ONE HTML file. The browser loads this, then Svelte JavaScript takes over rendering all content dynamically.
html
5. src/main.js — The Entry Point
This file bootstraps the Svelte application by mounting App.svelte to the DOM:
javascript
6. src/App.svelte — Root Component
This is the top of your component tree. It is the first component rendered:
svelte
7. src/lib/ — The Component Library Folder
The lib folder is the conventional home for all reusable components and utility functions. In SvelteKit, $lib is a special import alias:
text
8. vite.config.js — Build Configuration
vite.config.js
9. package.json — Project Manifest
json
10. public/ vs src/assets/
-
public/: Files placed here are served exactly as-is. Reference them with absolute paths:<img src="/logo.png">. Ideal for favicons and files that must not be processed.
-
src/assets/: Files here are processed by Vite (hashed filenames for cache-busting). Import them in your JavaScript:import logo from './assets/logo.svg'.
11. Recommended Folder Structure for Real Apps
text
12. Common Mistakes
-
Putting components in
src/root: As apps grow, dumping all components insrc/becomes chaotic. Always use thelib/components/structure from day one.
-
Confusing
public/andsrc/assets/: For images used in components, usesrc/assets/for the Vite optimization benefits (content hashing).
13. MCQs with Answers
Question 1
What is the entry JavaScript file for a Svelte/Vite application?
Question 2
What HTML element does Svelte mount the entire application into?
Question 3
What is the conventional folder for reusable components and utilities?
Question 4
What is the purpose of vite.config.js?
Question 5
What @sveltejs/vite-plugin-svelte does in vite.config.js?
Question 6
Files in the public/ folder are:
Question 7
What is the benefit of placing images in src/assets/ versus public/?
Question 8
In SvelteKit, what special alias allows importing from src/lib/ from anywhere in the project?
Question 9
What does nodemodules/ contain and should it be committed to Git?
Question 10
What command rebuilds nodemodules/ from package.json after cloning a project?
14. Interview Questions
-
Q: What is the purpose of the
libfolder in a Svelte project and how does it differ from page components?
-
Q: Explain the difference between files in
public/andsrc/assets/.
15. Summary
A well-organized Svelte project is a maintainable one. By understanding the roles ofmain.js (entry), App.svelte (root component), lib/ (reusable code), and public/ (static assets), you establish a solid architecture from day one that scales smoothly from prototype to production.