Skip to content
LATEST

🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.

Upgradix
Upgradix Code. Learn. Ship.
Updated Nov 25, 2026

Setting Up a Modern React Project with Vite

Vite is an open-source build tool that scaffolds a local React workspace in seconds. It serves files on demand using native browser ES modules during development and bundles fast production builds with Rolldown.

Setting Up a Modern React Project with Vite

Quick Answer: Vite is a fast, open-source build tool that starts a local React workspace in under a second. It uses native browser ES modules to update only the file you edit, replacing older, slower tools like Create React App.

Imagine Karthik is building a live Bengaluru bus tracking app for daily commuters. His app has 40 screens showing routes, timings, and live stops.

With an older tool like Create React App, every time Karthik starts his dev server, it bundles all 40 screens first. He waits 25 seconds just to see one small button change.

How can Karthik start his React workspace in milliseconds and see his edits on screen instantly?

1. Why Do We Use Vite Instead of Older Bundlers?

The problem without Vite

Older tools read your entire codebase and pack everything into one giant JavaScript bundle before the browser can even open the page.

Example 1: What happens with a traditional bundler on a growing app

This feels fine with 3 files, but slows down every time Karthik adds a new screen.

# Old workflow: bundles every file before starting
npm start

What happens during development

Compiling 40 React components...
Bundling routes, maps, and CSS...
Compiled successfully in 24,800 ms!

This setup works on day one. However, it hurts your focus as the project grows.

  1. Startup time grows slower with every new component you add.
  2. Saving a single line of text triggers a slow rebuild.
  3. Waiting 5 to 20 seconds breaks your coding rhythm.

The solution: Vite (Native ESM + Instant HMR)

Vite = a modern frontend build tool that serves files on demand using the browser's built-in module system.

HMR (Hot Module Replacement) = a feature that swaps only the edited component in your browser without a full page reload.

Instead of bundling all 40 screens upfront, Vite starts the server immediately. When Karthik edits BusStatus.jsx, Vite updates only that single file in milliseconds.

Live Flow: How Vite Updates Karthik's Bus Tracker in Milliseconds

Watch how editing one file bypasses full bundling and hot-swaps only the changed module.

Watching src/BusStatus.jsx... HMR: 14 ms
Karthik (VS Code)
Saves BusStatus.jsx
Vite Dev Server
Transforms 1 file via Native ESM
Browser (localhost:5173)
Hot-swaps UI · State kept
RouteMap.jsx (untouched)
BusStatus.jsx (reloaded)
FareCalc.jsx (untouched)
Developer Editor Vite Engine Live Browser Active HMR Step

Notice that RouteMap.jsx and FareCalc.jsx stay untouched. Vite only sends the changed BusStatus.jsx module over WebSocket.

2. How to Scaffold & Run Your React Workspace

Make sure you have Node.js version 20.19+ or 22.12+ installed on your computer. Then run one command to create Karthik's project.

Example 2: Creating and starting a React + Vite project

Run these commands in your terminal to scaffold the workspace and launch the dev server.

# 1. Scaffold a new React project using Vite
npm create vite@latest bengaluru-bus-tracker -- --template react

# 2. Move into the project folder and install packages
cd bengaluru-bus-tracker
npm install

# 3. Start the local development server
npm run dev

Expected output

  VITE v8.x.x  ready in 190 ms

  ->  Local:   http://localhost:5173/
  ->  Network: use --host to expose

Open http://localhost:5173/ in your browser. Your React workspace is already running in under a quarter of a second.

3. Inspect the Vite Project Structure

Vite keeps the folder layout clean and minimal. Here are the key files inside bengaluru-bus-tracker:

bengaluru-bus-tracker/
|-- index.html          # Entry HTML file (lives at the root, not public/)
|-- package.json        # Dependencies and npm scripts
|-- vite.config.js      # Vite settings and React plugin
|-- src/
    |-- main.jsx        # Mounts React into the #root div
    |-- App.jsx         # Your main React component
    |-- index.css       # Global styles

In Vite, index.html sits at the project root and loads /src/main.jsx directly with type="module". That is how the browser fetches files on demand.

4. Create React App vs. Vite

Legacy Create React App vs. Modern Vite setup
Create React App (Legacy) Vite (Modern Standard)
Bundles the entire app before starting. Starts instantly and serves files on demand.
HMR slows down as the project grows. HMR stays fast regardless of app size.
Deprecated and no longer maintained. Actively maintained, open-source, and lightweight.

The important terms to remember

  • Native ESM = the browser's built-in ability to load JavaScript import files directly.
  • HMR = Hot Module Replacement, which updates edited components without losing app state.
  • Production Build = running npm run build to create a minified dist/ folder for deployment.

Design tip: Vite skips bundling during development for speed, and when you run npm run build, Vite 8 uses its Rust-powered Rolldown bundler to create a fast, optimized production bundle.

5. Common Mistakes to Avoid

1. Moving index.html into the public/ folder

Developers used to Create React App often drag index.html into public/. Vite expects index.html at the root and will fail to load your app.

Better approach: Always keep index.html in the root directory of your Vite project.

2. Using .js instead of .jsx for files with JSX

Vite strictly expects React files containing HTML-like syntax to end in .jsx (or .tsx for TypeScript).

Better approach: Name every React component file with the .jsx extension, like BusStatus.jsx.

6. Interview Questions & Practice

Question 1: Why is Vite so much faster than Webpack or Create React App during development?

What the interviewer expects: Clear understanding of native ES modules and on-demand serving vs. full upfront bundling.

Strong answer: "Older tools like Create React App bundle every file in Karthik's 40-screen bus app before starting the dev server. Vite skips upfront bundling and serves files on demand using native browser ES modules, so startup takes milliseconds and HMR only reloads the one file you edited."

Question 2: Does Vite also skip bundling when you deploy to production?

What the interviewer expects: Knowing that unbundled files are great for local dev, but production still needs bundling.

Strong answer: "No. Loading hundreds of separate files over a mobile network would be slow. When we run npm run build, Vite bundles, tree-shakes, and minifies the code into a small dist/ folder for fast production loading."

What interviewers expect at different levels

  • Junior developer: Knows how to scaffold a React app with npm create vite@latest, explain index.html and src/main.jsx, and run npm run dev.
  • Senior developer: Explains Native ESM, HMR, environment variables (VITE_ prefix), and how vite.config.js configures proxies and plugins.
  • Technical architect: Evaluates build performance, code splitting, production chunk optimization, and migrating legacy CRA apps to Vite.

Beginner: Hospital appointment portal

Scaffold a new Vite React app named chennai-clinic-app, start the dev server, and change the main heading in src/App.jsx to "Chennai Clinic Appointments".

Expected result: The browser at http://localhost:5173/ updates instantly without a full page reload.

Intermediate: UPI payment dashboard

Create a Vite React project with the TypeScript template (react-ts) and add a new component file src/TransactionList.tsx.

Hint: Pass -- --template react-ts when running npm create vite@latest.

Advanced: Festival flash sale storefront

Build a production-ready Vite React workspace and verify how it behaves before deployment:

  • Run npm run build and inspect the generated dist/assets/ folder.
  • Run npm run preview to serve the production build locally.

Challenge: Notice the hashed filenames (like index-a1b2c3.js) in dist/assets/ and explain how they help with browser caching.

7. Frequently Asked Questions

Why does Vite use port 5173 by default?

The number 5173 spells "VITE" in leetspeak (5=V/S, 1=I, 7=T, 3=E). You can change it anytime in vite.config.js.

Can I use TypeScript with Vite and React?

Yes. Just run npm create vite@latest my-app -- --template react-ts and Vite sets up TypeScript out of the box.

8. Next Steps

  • Open src/App.jsx in your new workspace, edit the title, and watch HMR update the browser live.
  • Run npm run build and npm run preview to test your first production-ready bundle locally.

Discussion & Comments

0
Guest mode · Login / Register
Post directly as a guest or log in first.

No comments yet

Start the discussion above — ask a question, share a tip, or reply to a comment.

This article is protected. Printing and saving as PDF are disabled.