React / 07
A real React project with Vite
One command creates a React project with a dev server, a production build and sensible defaults. Here's what each file does, plus the Parcel way.
Create the project
You need Node.js 20.19+ or 22.12+. Then:
Scaffold
npm create vite@latest tasty-bites -- --template reactcreates a folder with everything in it. The--passes--templatethrough npm to the create tool.Install
cd tasty-bitesthennpm installdownloads React, Vite and the rest intonode_modules.Run
npm run devstarts the dev server. Openhttp://localhost:5173.
Leave out --template and it asks you questions instead. react-ts gives the same thing with TypeScript.
What you get
- tasty-bites/
- index.htmlentry point: has #root and loads main.jsx
- package.jsonscripts + dependencies
- vite.config.jsturns on the React plugin
- .gitignorenode_modules, dist…
- public/copied as-is (favicon)
- src/
- main.jsxcreateRoot(...).render(<App />)
- App.jsxyour first component
- index.css, App.css
- assets/images you import
1 · browser asks for /
index.html2 · the entry script
main.jsx3 · your UI
<App />Three files do the real work:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Tasty Bites</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>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>
);import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
});index.htmlis the entry point. Vite starts here and follows the<script>tomain.jsx.main.jsxis the samecreateRoot+renderfrom Chapter 1, now rendering a component.StrictModeadds extra development-only checks.vite.config.jsturns on the React plugin, which compiles JSX and enables Fast Refresh.
The scripts
| Script | What it does |
|---|---|
npm run dev | Dev server at localhost:5173 with instant updates |
npm run build | Production build into dist/ |
npm run preview | Serves dist/ at localhost:4173 so you can test the real build |
npm run lint | Checks your code for mistakes (the template ships a linter) |
To deploy, you upload dist/. It's just static files, so any static host can serve it.
The same app with Parcel
Parcel needs no config file at all. Setting it up by hand shows every piece:
mkdir tasty-bites && cd tasty-bites
npm init -y
npm install react react-dom
npm install -D parcel
# create src/index.html and src/main.jsx like the Vite ones
npm start # dev server at http://localhost:1234{
"name": "tasty-bites",
"source": "src/index.html",
"scripts": {
"start": "parcel",
"build": "parcel build"
},
"dependencies": { "react": "^19.3.1", "react-dom": "^19.3.1" },
"devDependencies": { "parcel": "^2.16.4" }
}node_modules
dist
.parcel-cacheThe source field tells Parcel where to start, so the scripts don't need a file name. Without it you'd write parcel src/index.html. Parcel spots React in your dependencies and compiles JSX automatically.
Gotchas
Recall
Can you answer these?
0/5
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
