Setting up with Vite
Create a React project with Vite, tour the files, run the dev server and build for production.
To build React apps you need a tool that compiles JSX, bundles your modules, and gives you a fast development server with instant updates. The standard choice today is Vite (French for "quick", pronounced "veet"). In this lesson you'll create a project, understand every file it generates, and learn the commands you'll use daily.
What about Create React App? CRA was the old default but has been deprecated since 2025. Use Vite for single-page apps, or a framework such as Next.js when you need server rendering (covered at the end of this course).
Prerequisites#
You need Node.js (a current LTS such as Node 24) and npm. Check:
You'll also want a code editor — VS Code is the most popular — and a modern browser with the React Developer Tools extension (Chrome, Edge or Firefox).
Create a project#
npm create vite@latestdownloads and runs Vite's project generator.--template reactchooses React with JavaScript. Use--template react-tsfor TypeScript (recommended once you've done the TypeScript lesson). Without--template, it asks you interactively.npm installdownloads React, Vite and other dependencies intonode_modules/.npm run devstarts the dev server.
Open http://localhost:5173 and you'll see the starter page with a counter button. Leave the server running while you work.
A tour of the project#
(The generator also adds a .gitignore, a linter config such as .oxlintrc.json, and a README.)
index.html
Unlike older tools, Vite treats index.html as the real entry point. The empty <div id="root"> is where React will render your whole app, and the module script loads your code.
src/main.jsx
reactcontains the core (components, hooks);react-dom/clientconnects React to the browser DOM.createRoot(element)creates a React root inside#root, and.render(<App />)draws your app into it.<StrictMode>enables extra development-only checks — it intentionally runs some code twice to reveal bugs. It has no effect in production.- Importing a CSS file (
import "./index.css") makes Vite inject it into the page.
src/App.jsx
Replace the starter with something minimal:
Save, and the browser updates instantly without reloading — and without losing the counter's value. That's Hot Module Replacement (HMR).
package.json
(Exact versions depend on when you create the project. Current templates use Oxlint, a very fast linter; older ones use ESLint.)
vite.config.js
The React plugin enables JSX and Fast Refresh. You'll add more here later — Tailwind, path aliases, a dev proxy for your API.
Organising your src folder#
There's no single "correct" structure. A common one as the app grows:
Conventions:
- One component per file, named in PascalCase:
CourseCard.jsxexportsCourseCard. - Files containing JSX use the
.jsx(or.tsx) extension. - Keep components that change together close together.
Static assets#
Files in src/assets are processed (hashed file names for caching, small images inlined). Files in public/ are served as-is at the root: public/robots.txt → /robots.txt.
Environment variables#
Create .env (or .env.local, which git should ignore) in the project root:
Only variables starting with VITE_ are exposed to your code — and anything exposed ends up in the JavaScript bundle where anyone can read it. Never put secrets in front-end environment variables.
Building for production#
dist/ contains plain static files — HTML, CSS and JavaScript — that any static host can serve. You'll deploy them in the Deployment lesson.
Recommended editor setup#
- VS Code extensions: your linter's extension (Oxlint or ESLint), Prettier, and optionally "ES7+ React snippets".
- React Developer Tools browser extension: adds Components and Profiler tabs to DevTools so you can inspect props, state and renders.
- Turn on format on save with Prettier.
Common mistakes#
- Opening
index.htmldirectly from the file system instead of usingnpm run dev— modules and JSX need the dev server. - Running
npm run devin the wrong folder (cd my-react-appfirst). - Forgetting to restart the dev server after editing
.envorvite.config.js. - Naming a file
app.jsand putting JSX in it — use.jsx. - Committing
node_modules/ordist/.
What's next#
Your project is running. Next, learn JSX properly — the syntax you'll write in every component.
Check your understanding
Quick quiz
1.Which command creates a new React project with Vite?
2.In a Vite project, which file is the entry point that mounts React into the page?
3.How do you expose an environment variable to your React code in Vite?
Finished reading?
Mark this lesson complete to track your progress.