Start a React project with Vite
Create a React project with Vite, run it in the browser, understand each file, and clean it up into the Brew & Bean starting point. This is the base for every later lesson.
In this lesson you will create a real React project, run it in your browser and edit it live. You will end with a clean starting point called brew-and-bean, the coffee shop website the next lessons build on.
Prerequisites: you have read *How the web works*, you know basic HTML and JavaScript (the previous two lessons), and you can open a terminal. You need Node.js and a code editor such as VS Code. Both are free.
What Vite does and why we use it
Browsers understand HTML, CSS and JavaScript. They do not understand JSX (the HTML-like syntax React uses), and a real app is split across many files that must be combined. A build tool does that work for you.
Vite (pronounced "veet") is a build tool with a fast development server. You run one command, it serves your app at a local address, and every time you save a file the browser updates without a full reload. When you finish, Vite also builds a small optimised bundle for deployment.
React itself is only a library for building user interfaces. Vite gives it a project around it.
A note on alternatives: full frameworks such as Next.js are often used for production apps, and you will meet Next.js later in this track. Starting with Vite is a good way to learn React itself, with nothing hidden.
Install the tools
Setup
- 1Install the LTS version of Node.js from nodejs.org. It includes
npm, the package manager that downloads libraries. - 2Open a terminal (Terminal on macOS and Linux, PowerShell or Windows Terminal on Windows) and run
node --version. Vite needs a reasonably recent Node.js; ifnpm createlater complains about the Node version, install the current LTS. - 3Run
npm --versionto confirm npm works. - 4Install VS Code from code.visualstudio.com if you do not have an editor yet.
node --version
npm --versionEach command prints a version number such as v22.x.x and 10.x.x. Your numbers will differ.
Create the project
npm create vite@latest brew-and-bean -- --template reactCreates a folder called brew-and-bean using Vite's plain JavaScript React template. The -- passes the template option on to Vite.
Vite may ask a few questions, such as whether to install and start right away. The exact prompts change between versions. Choose plain JavaScript if asked (not TypeScript; you will add TypeScript later in this track), and answer "No" if it offers to start the dev server, so you can follow each step below.
cd brew-and-bean
npm install
npm run devnpm install downloads the libraries listed in package.json into node_modules. npm run dev starts the development server.
VITE vX.X.X ready in 300 ms
Local: http://localhost:5173/
press h + enter to show helpOpen http://localhost:5173/ in your browser. You should see the Vite and React logos and a button that counts clicks. Leave the terminal running. Press Ctrl + C in it when you want to stop the server.
Common start-up problems
If the port is busy, Vite picks another one (for example 5174) and prints it. Always use the address printed in your terminal. If you see "npm: command not found", Node.js is not installed or you need to reopen the terminal.
A tour of the project
What is in the folder
| File or folder | What it is |
|---|---|
| index.html | The one HTML page. It has an empty <div id="root"> and loads src/main.jsx. |
| package.json | Project name, scripts (dev, build, preview) and the libraries it depends on. |
| node_modules/ | Downloaded libraries. Huge. Never edit it and never commit it to Git. |
| src/main.jsx | The entry point. It finds #root and tells React to render <App /> into it. |
| src/App.jsx | Your first component: the top of your app. |
| src/index.css and src/App.css | Stylesheets. |
| public/ | Files served as they are (favicon, images referenced by URL). |
| vite.config.js | Vite settings. You rarely touch it at the start. |
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import "./index.css";
import App from "./App.jsx";
createRoot(document.getElementById("root")).render(
<StrictMode>
<App />
</StrictMode>
);This file connects React to the page. createRoot takes the empty div from index.html and render draws your App inside it. StrictMode is a development helper that warns about risky code; it has no effect on the final site. Your generated file may differ slightly.
Notice what the whole app is: index.html has an empty box, JavaScript fills it. This is a single-page app. The browser loads one HTML file, and React changes what is inside the box as the user interacts.
Clean it up for Brew & Bean
The template includes demo content. Replace it with your own so you understand every line.
Replace the demo
- 1Open the project folder in VS Code (
code .from the terminal, or File > Open Folder). - 2Open
src/App.cssand delete everything in it (the file can stay empty). - 3Open
src/index.cssand replace its contents with the CSS below. - 4Open
src/App.jsxand replace its contents with the component below. - 5Delete the
src/assetsfolder if you like; nothing uses it now.
body {
margin: 0;
font-family: system-ui, sans-serif;
background: #fffaf3;
color: #2b1d14;
}
h1 {
margin: 0;
padding: 24px;
background: #4a2c1a;
color: #fff;
}
p {
padding: 0 24px;
line-height: 1.6;
}export default function App() {
return (
<div>
<h1>Brew & Bean</h1>
<p>Fresh coffee in Indiranagar, Bengaluru. Open 8 am to 10 pm. Pickup only.</p>
</div>
);
}App is a function that returns what should appear on screen. The tag-like syntax inside is JSX. Components get a full lesson next.
Save the file. The browser updates by itself within a moment, with no refresh. This is hot module replacement: Vite swaps only the changed code into the running page.
A brown bar at the top with the white heading "Brew & Bean", and the sentence about Indiranagar underneath on a cream background.Learn from errors
Change the text in App.jsx, save, and watch the page. Break it on purpose (delete a closing tag) and read the error: Vite shows it as an overlay in the browser with the file and line number. Reading that overlay is the most useful early debugging habit.
The three scripts you will use
package.json scripts
| Command | What it does |
|---|---|
| npm run dev | Starts the development server with hot reloading. Use this while you build. |
| npm run build | Creates the production bundle in the dist folder: minified and ready to deploy. |
| npm run preview | Serves the dist folder locally so you can check the production build. |
npm run build
npm run previewbuild writes dist/ with index.html and hashed JavaScript and CSS files. preview serves it, usually at http://localhost:4173.
You will deploy the dist folder in the last module. You never edit files inside dist; it is regenerated on every build.
Two habits that save time
File names. Files that contain JSX should end in .jsx. Component file names start with a capital letter (App.jsx, MenuCard.jsx).
Git. Vite creates a .gitignore that already excludes node_modules and dist. If you want to put the project on GitHub, run git init, commit, and push. Never commit node_modules; anyone can recreate it with npm install.
I'm learning React with Vite (plain JavaScript). I ran [command] and got this error:
---
[paste the full error text]
---
My Node.js version is [output of node --version]. Explain in simple words what the error means, give the most likely cause, and give me steps to fix it. Do not rewrite my whole project.What to expect: Check that the answer explains the cause and not just a command to paste. Run only commands you understand, and prefer fixes that change one thing at a time.
Try it: Make it yours
Extend the page: add a second heading <h2>Today's menu</h2> below the paragraph, then a paragraph about the opening hours. Then run npm run build and look inside the dist folder.
Check yourself
npm run devshows the page at the localhost address- Saving a file updates the browser without a manual refresh
- You can find index.html, main.jsx and App.jsx and say what each does
npm run buildcreates adistfolder- node_modules is not committed to Git
Key takeaways
- Vite is a build tool and dev server; React is the library that runs inside the project it creates.
npm create vite@latest <name> -- --template reactmakes a project;npm installthennpm run devruns it.- index.html holds an empty root div; main.jsx renders
<App />into it. - Edits appear instantly through hot module replacement; read the error overlay when something breaks.
npm run buildproduces thedistfolder you deploy; never edit it or commit node_modules.
Check yourself
Finishing the quiz marks the lesson as done.
1. What is the job of src/main.jsx in a Vite React project?
2. Which command starts the development server with hot reloading?
3. You run npm run build. Where is the deployable output?
4. Why should node_modules not be committed to Git?
5. You save App.jsx and the page updates without refreshing. What is this called?
Saved on this device. Create a free account to keep it on every device.
Practise this with Sam
A real mock interview, out loud, with an honest scorecard. Free to start.