Skip to content
Getting StartedChapter 2 / 37Beginner4 min read

Setup with Vite

Learn the React Vite setup step by step, from creating a new project with npm create vite to the folder structure, main.jsx and createRoot.

On this page

1. What is Vite?

React by itself is only a library. The browser does not understand JSX directly, so you need a tool that turns JSX into JavaScript, serves your files and makes the production build. Today that job is done by Vite.

The old way was Create React App (CRA). The React team officially deprecated it in 2025, so for new projects use Vite (or a framework like Next.js).

2. Why does it matter?

Old setup (CRA)Vite
The dev server takes a long time to startStarts almost instantly
The whole bundle is rebuilt on every changeOnly the changed file updates
Hidden and hard-to-read configA small and clear vite.config.js
No longer maintainedActively maintained and modern

In development, Vite uses the browser's native ES modules and processes only the files the browser asks for. That is why HMR (Hot Module Replacement) updates the screen as soon as you save, and most of the time the state is kept too.

3. Where is it used?

ProjectWhy Vite
Learning projectsOne command, zero config
Single page apps (SPA)Dashboards, admin panels, internal tools
Component librariesBuild a package with library mode
Other frameworksVue, Svelte and Solid also use Vite

If you need routing, server rendering and SEO built in, a framework like Next.js is better. For learning and SPAs, Vite is perfect.

4. How does it work?

Vite React app flow
npm run dev
The Vite dev server runs on localhost:5173
The browser loads index.html
index.html loads src/main.jsx
createRoot renders App into the div with id root

Folder structure

FileJob
index.htmlThe entry point, it has div id="root"
src/main.jsxConnects React to #root
src/App.jsxYour first component
public/Static files served as they are
vite.config.jsVite settings and the React plugin
package.jsonScripts and dependencies

5. Code example

Creating the project

First install the current LTS version of Node.js, then:

bash
npm create vite@latest my-app -- --template react
cd my-app
npm install
npm run dev

If you want TypeScript, use the react-ts template.

src/main.jsx

jsx
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

jsx
export default function App() {
  return (
    <main>
      <h1>Hello React</h1>
      <p>My first Vite project</p>
    </main>
  );
}

How does the code work?

ItemMeaning
npm create vite@latestRuns Vite's latest scaffolding tool
--template reactThe React + JavaScript starter
createRootThe root API of React 18+, hands the #root element over to React
.render(...)Shows the App component on the screen
StrictModeExtra checks in development, no effect in production
npm run buildOptimized files in the dist/ folder
npm run previewTest the dist/ build locally

Write environment variables in the .env file with the VITE_ prefix and read them with import.meta.env.VITE_API_URL.

Interview tip

"What is the difference between Create React App and Vite?" CRA was built on webpack and is now deprecated. Vite gives native ES modules and fast HMR in dev, and makes the production build with a separate optimized bundler.

6. Common mistakes

Mistake: Using create-react-app for a new project

Why? CRA is deprecated, with outdated dependencies and warnings.

Better: Use npm create vite@latest, or Next.js for a full-stack app.

Mistake: Running npm run dev without running npm install

Why? There is no node_modules, so you get an error like vite command not found.

Better: After creating the project, run npm install first, then npm run dev.

Mistake: Writing an env variable without the VITE_ prefix

Why? For security, Vite exposes only variables with VITE_ to browser code, and the rest come out undefined.

Better: Start browser variables with VITE_, and never keep secret keys in the frontend .env.

Mistake: Copying ReactDOM.render from an old tutorial

Why? ReactDOM.render has been removed in React 19.

Better: Import createRoot from react-dom/client and use root.render.

7. What to learn before and after

Final quick revision

PointDetail
ViteA fast dev server and build tool
Commandnpm create vite@latest my-app -- --template react
Entryindex.html to src/main.jsx
Root APIcreateRoot(...).render(...)
Dev portlocalhost:5173
CRADeprecated, do not use it