Web Development with React
Hands-onBeginner 30 min· Web foundations · 4 of 21

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

  1. 1Install the LTS version of Node.js from nodejs.org. It includes npm, the package manager that downloads libraries.
  2. 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; if npm create later complains about the Node version, install the current LTS.
  3. 3Run npm --version to confirm npm works.
  4. 4Install VS Code from code.visualstudio.com if you do not have an editor yet.
Terminal
node --version
npm --version

Each command prints a version number such as v22.x.x and 10.x.x. Your numbers will differ.

Create the project

Terminal
npm create vite@latest brew-and-bean -- --template react

Creates 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.

Terminal
cd brew-and-bean
npm install
npm run dev

npm install downloads the libraries listed in package.json into node_modules. npm run dev starts the development server.

What you should see in the terminal (version number will differ)
  VITE vX.X.X  ready in 300 ms

  Local:   http://localhost:5173/
  press h + enter to show help

Open 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 folderWhat it is
index.htmlThe one HTML page. It has an empty <div id="root"> and loads src/main.jsx.
package.jsonProject 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.jsxThe entry point. It finds #root and tells React to render <App /> into it.
src/App.jsxYour first component: the top of your app.
src/index.css and src/App.cssStylesheets.
public/Files served as they are (favicon, images referenced by URL).
vite.config.jsVite settings. You rarely touch it at the start.
src/main.jsx
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

  1. 1Open the project folder in VS Code (code . from the terminal, or File > Open Folder).
  2. 2Open src/App.css and delete everything in it (the file can stay empty).
  3. 3Open src/index.css and replace its contents with the CSS below.
  4. 4Open src/App.jsx and replace its contents with the component below.
  5. 5Delete the src/assets folder if you like; nothing uses it now.
src/index.css
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;
}
src/App.jsx
export default function App() {
  return (
    <div>
      <h1>Brew &amp; 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.

Output
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

CommandWhat it does
npm run devStarts the development server with hot reloading. Use this while you build.
npm run buildCreates the production bundle in the dist folder: minified and ready to deploy.
npm run previewServes the dist folder locally so you can check the production build.
Terminal
npm run build
npm run preview

build 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.

Prompt · Explain an error
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

  1. npm run dev shows the page at the localhost address
  2. Saving a file updates the browser without a manual refresh
  3. You can find index.html, main.jsx and App.jsx and say what each does
  4. npm run build creates a dist folder
  5. node_modules is not committed to Git

Key takeaways

  1. Vite is a build tool and dev server; React is the library that runs inside the project it creates.
  2. npm create vite@latest <name> -- --template react makes a project; npm install then npm run dev runs it.
  3. index.html holds an empty root div; main.jsx renders <App /> into it.
  4. Edits appear instantly through hot module replacement; read the error overlay when something breaks.
  5. npm run build produces the dist folder 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.

Start free