Lesson 24 of 37Real projects
Project Setup
Create a React app with Vite, know what every generated file does, and organize it so it still makes sense at 200 files.
You now know React from its basics to its newest features, but until now your code has run in a preview window. Real projects live on your machine, with a dev server, a build step, environment variables, and a folder structure that a team of five can navigate. On your first day at a job you will clone a repo like this, and in take-home assignments a clean setup is the first thing a reviewer sees.
Create a project with Vite
Vite (French for "fast", pronounced "veet") is the standard tool for client-side React apps. It gives you a dev server that starts in milliseconds and a production build that bundles and minifies your code. You need Node.js installed (an even-numbered LTS release such as 22 or 24).
npm create vite@latest my-app -- --template react-tscd my-appnpm installnpm run devnpm create vite@latest downloads the newest scaffolding tool and asks no questions because --template react-ts picks React with TypeScript. (The extra -- passes the flag through npm to Vite.) The dev server prints a URL, usually http://localhost:5173.
What every file does
- my-app/
- index.htmlthe real entry point: has <div id="root"> and loads /src/main.tsx
- package.jsondependencies and npm scripts (dev, build, lint, preview)
- package-lock.jsonexact installed versions; commit it
- vite.config.tsVite settings and plugins (@vitejs/plugin-react)
- tsconfig.jsonpoints to the two configs below
- tsconfig.app.jsonTypeScript settings for your src/ code
- tsconfig.node.jsonTypeScript settings for vite.config.ts
- eslint.config.jslint rules, including react-hooks
- .gitignorekeeps node_modules/ and dist/ out of git
- public/copied as-is to the build; referenced as /vite.svg
- vite.svg
- src/
- main.tsxcreateRoot(document.getElementById("root")).render(<App />)
- App.tsxyour top-level component
- App.cssstyles for App
- index.cssglobal styles
- assets/images imported from code get hashed file names
- react.svg
Two files deserve a closer look. Unlike older tools, Vite treats index.html as source code, so the script tag points straight at your TypeScript:
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>And main.tsx connects React to that div. This is the only place your app touches the page directly:
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App.tsx";
import "./index.css";
createRoot(document.getElementById("root")!).render(
<StrictMode>
<App />
</StrictMode>
);The scripts in package.json:
| Script | Runs | What it does |
|---|---|---|
npm run dev | vite | Dev server with hot module replacement |
npm run build | tsc -b && vite build | Type-checks, then bundles to dist/ |
npm run preview | vite preview | Serves dist/ locally to test the real build |
npm run lint | eslint . | Checks code with the lint rules |
logo.png. When should it go in public/ rather than src/assets/?Dev server, HMR, and the build
In development, Vite does not bundle. It serves each file as a native ES module and transforms TypeScript and JSX on request, which is why it starts instantly. When you save a file, hot module replacement (HMR) swaps just that module in the running page. With the React plugin (Fast Refresh), component state survives the edit.
npm run build is different: it bundles everything into a few optimized files.
$ npm run builddist/index.html 0.46 kBdist/assets/index-DiwrgTda.css 1.39 kBdist/assets/index-BxH3kq1z.js 195.12 kB │ gzip: 61.04 kBdist/ is plain static files. You can host it anywhere that serves files: Netlify, Vercel, Cloudflare Pages, or an S3 bucket behind a CDN.
Environment variables
Configuration that changes between environments (an API URL, a public analytics key) goes in .env files, not in code:
VITE_API_URL=http://localhost:8000DATABASE_URL=postgres://... # NOT exposed to the browserIn code you read import.meta.env.VITE_API_URL. Vite only exposes variables that start with VITE_, and it replaces them with their literal values at build time. Anything in a client bundle is public: anyone can open DevTools and read it. Never put secret keys in a VITE_ variable.
Here is what Vite does conceptually during a build. Run it and change the mode.
Press Run (⌘ Enter) to see what happens..env contains API_URL=/api. What does this log in the browser?
console.log(import.meta.env.API_URL);
When to choose Next.js instead
Vite gives you a single-page app (SPA): the server sends an empty div and JavaScript builds the page in the browser. That is ideal for dashboards, internal tools, and apps behind a login.
Pick a framework like Next.js when you need things a static SPA cannot do well: pages that must be indexed by search engines with content in the first HTML response, server-side data fetching close to your database, server-only code for secrets, or file-based routing with layouts out of the box. The React docs recommend starting new full-stack apps with a framework. You can still use everything from this track inside one.
A folder structure that scales
The template's flat src/ is fine for ten files. Past that, group code by feature (what it is for), not by kind (components here, hooks there). Everything about todos lives together, so deleting or changing a feature touches one folder.
- src/
- app/
- App.tsxproviders, router, layout
- routes.tsx
- features/
- todos/
- components/
- TodoList.tsx
- TodoItem.tsx
- hooks/
- useTodos.tsdata + state logic for todos
- api/
- todos.tsfetchTodos, createTodo
- types.ts
- index.tsthe feature's public surface
- auth/
- components/
- hooks/
- api/
- components/
- ui/shared, feature-free building blocks
- Button.tsx
- Dialog.tsx
- lib/framework-free helpers: formatDate, apiClient
- main.tsx
Rules that keep it clean: features may import from components/ui and lib, but not reach into each other's internals; components/ui knows nothing about features; data access lives in api/, not inside components. Here is the same shape squeezed into one runnable file so you can see the layers:
Absolute imports, linting, formatting
Deep relative paths like ../../../components/ui/Button break when you move files. Set up an @ alias for src/ in both TypeScript and Vite:
{
"compilerOptions": {
"paths": { "@/*": ["./src/*"] }
}
}import path from "node:path";
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [react()],
resolve: { alias: { "@": path.resolve(__dirname, "./src") } },
});Now import { Button } from "@/components/ui/Button" works anywhere. TypeScript needs paths to type-check it; Vite needs alias to actually resolve it.
ESLint (already in the template) catches bugs: eslint-plugin-react-hooks enforces the rules of hooks and missing dependencies. Prettier formats code so nobody argues about semicolons; add it with npm install -D prettier and run npx prettier . --write, or turn on format-on-save in your editor. Biome is a fast all-in-one alternative that does both.
Match the file to its job.
What Vite exposes to the browser
Vite only puts environment variables whose names start with VITE_ into import.meta.env, so secrets like DATABASE_URL never reach the browser bundle. It also adds a few built-ins.
Write publicEnv(env, mode) that returns what client code would see:
- every key in
envthat starts withVITE_(value unchanged) MODE: themodestringDEV:truewhenmodeis"development"PROD:truewhenmodeis"production"
publicEnv({ VITE_API_URL: "/api", DB_PASSWORD: "hunter2" }, "development")
→ { VITE_API_URL: "/api", MODE: "development", DEV: true, PROD: false }- Vite
- A build tool with an instant ES-module dev server and an optimized production bundler.
- HMR
- Hot module replacement: swapping an edited module into the running page, keeping React state with Fast Refresh.
- import.meta.env
- Where Vite exposes
VITE_variables and built-ins likeMODE, replaced with literal values at build time. - SPA
- Single-page app: the server sends a shell and JavaScript renders every page in the browser.
- feature folder
- Grouping components, hooks, and API code for one feature together, such as
features/todos/. - path alias
- A short import prefix like
@/mapped tosrc/in bothtsconfigandvite.config.ts.