React · Getting Started

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.

Beginner 5 min Lesson 3 of 42Examples target React 19 with Vite.
Before you start:Introduction to React
0/42 in React
On this page · 5 min · Beginner

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#

Shell Illustrative
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#

JSXsrc/main.jsx Illustrative
// 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>
  );
}
Expected output
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#

  1. index.html contains <div id="root"></div> and loads /src/main.jsx as a module.
  2. createRoot attaches React to that div; render(<App />) draws the App component inside it.
  3. StrictMode runs extra development checks (it renders components twice in dev to reveal side effects).
  4. 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#

Opening index.html directly

Double-clicking index.html does not run Vite’s transforms. Use npm run dev.

Old Node.js versions

Vite needs a recent Node.js LTS. “Unexpected token” or “crypto.getRandomValues” errors usually mean Node is too old.

Using Create React App tutorials

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?
Use the react template, edit src/App.jsx, then npm run build.
Show solution
Shell Illustrative
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#

React Setup with Vite quizMultiple choice Question 1 of 3

Which command starts the Vite dev server?

Score so far: 0/0

Want more? Practise all React MCQs · React interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.