React Setup with Vite
Vite is the fastest way to start a React app. One command creates the project, and its dev server shows every change in the browser almost instantly.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
Related tutorials
Learning objectives#
- Create a React project with Vite
- Understand the files in a new project
- Run the dev server and see hot reload
- Build and preview a production version
Explanation#
You need Node.js (LTS). Then npm create vite@latest my-app -- --template react (or react-ts for TypeScript) scaffolds a project.
npm install downloads dependencies; npm run dev starts the dev server at http://localhost:5173 with hot module replacement — save a file and the page updates without losing state.
index.html is the entry page; src/main.jsx mounts the app with createRoot(document.getElementById("root")).render(<App />); src/App.jsx is your first component.
npm run build produces an optimised dist/ folder (minified JS, hashed file names); npm run preview serves it locally to test.
Create React App is deprecated. For server rendering and file-based routing, React’s docs recommend frameworks such as Next.js or React Router; Vite is ideal for client-side apps and learning.
Syntax#
npm create vite@latest shop-ui -- --template react
cd shop-ui
npm install
npm run dev
Code example: The two files every Vite React app starts with#
// src/main.jsx — mounts React into <div id="root"> in index.html
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.jsx';
import './index.css';
createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>,
);
// src/App.jsx — your first component (edit it and watch the browser update)
export default function App() {
return (
<main>
<h1>Shop UI</h1>
<p>Edit <code>src/App.jsx</code> and save to see hot reload.</p>
</main>
);
}
npm run dev prints “Local: http://localhost:5173/”. The browser shows “Shop UI” and the hint text. Changing the heading in App.jsx and saving updates the page in well under a second.
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- index.html contains <div id="root"></div> and loads /src/main.jsx as a module.
- createRoot attaches React to that div; render(<App />) draws the App component inside it.
- StrictMode runs extra development checks (it renders components twice in dev to reveal side effects).
- App.jsx exports the component that main.jsx imports.
Real-world use case#
A team starts every new internal dashboard with the Vite react-ts template, adds React Router and a UI library, and deploys the dist/ folder to static hosting.
Common mistakes#
Double-clicking index.html does not run Vite’s transforms. Use npm run dev.
Vite needs a recent Node.js LTS. “Unexpected token” or “crypto.getRandomValues” errors usually mean Node is too old.
CRA is deprecated; commands like react-scripts start do not apply to Vite projects.
Best practices#
- Start with the react-ts template when you can
- Keep main.jsx minimal
- Commit package-lock.json
- Test the production build with npm run preview
Remember this#
npm create vite@latest → npm install → npm run dev.
main.jsx mounts; App.jsx is your first component.
npm run build → dist/ for deployment.
Practice exercise#
Your task: Create a Vite React app called todo-app, change App.jsx to show "My Todos", and build it for production.
Need a hint?
Show solution
npm create vite@latest todo-app -- --template react
cd todo-app
npm install
# edit src/App.jsx: return <h1>My Todos</h1>;
npm run dev # check it in the browser
npm run build # creates dist/
npm run preview # serves dist/ locally
Mini quiz#
Which command starts the Vite dev server?
Want more? Practise all React MCQs · React interview questions
My notes#
Saved in this browser.