Setup with Vite
React vite setup step by step, npm create vite se naya project, folder structure, main.jsx aur createRoot ko Roman Urdu mein samjhein.
On this page
1. Vite kya hai?
React khud sirf ek library hai. Browser JSX ko seedha nahi samajhta, is liye ek tool chahiye jo JSX ko JavaScript mein badle, files ko serve kare aur production build banaye. Aaj kal yeh kaam Vite karta hai.
Purana tareeqa Create React App (CRA) tha. React team ne usay 2025 mein officially deprecate kar diya hai, is liye naye projects mein Vite (ya Next.js jaisa framework) use karein.
2. Kyun zaroori hai?
| Purana setup (CRA) | Vite |
|---|---|
| Dev server start hone mein kaafi waqt | Lagbhag foran start |
| Har change par poora bundle dobara | Sirf badli hui file update |
| Config chhupa hua aur mushkil | Chhota aur saaf vite.config.js |
| Ab maintain nahi hota | Actively maintained aur modern |
Vite development mein browser ke native ES modules use karta hai aur sirf wohi file process karta hai jo browser maangta hai. Isi liye HMR (Hot Module Replacement) se save karte hi screen update ho jati hai, state bhi zyada tar qaim rehti hai.
3. Kahan use hota hai?
| Project | Vite kyun |
|---|---|
| Seekhne ke projects | Ek command, zero config |
| Single page apps (SPA) | Dashboards, admin panels, internal tools |
| Component libraries | Library mode se package build |
| Doosre frameworks | Vue, Svelte aur Solid bhi Vite use karte hain |
Agar aapko routing, server rendering aur SEO built-in chahiye to Next.js jaisa framework behtar hai. Seekhne aur SPA ke liye Vite perfect hai.
4. Kaise kaam karta hai?
Folder structure
| File | Kaam |
|---|---|
index.html | Entry point, is mein div id="root" hota hai |
src/main.jsx | React ko #root se jorta hai |
src/App.jsx | Aap ka pehla component |
public/ | Static files jo jaisi hain waisi serve hoti hain |
vite.config.js | Vite ki settings aur React plugin |
package.json | Scripts aur dependencies |
5. Code example
Project banana
Pehle Node.js ka current LTS version install karein, phir:
npm create vite@latest my-app -- --template react
cd my-app
npm install
npm run devTypeScript chahiye to template react-ts likhein.
src/main.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
export default function App() {
return (
<main>
<h1>Salam React</h1>
<p>Mera pehla Vite project</p>
</main>
);
}Code kaise kaam kar raha hai?
| Cheez | Matlab |
|---|---|
npm create vite@latest | Vite ka latest scaffolding tool chalata hai |
--template react | React + JavaScript wala starter |
createRoot | React 18+ ka root API, #root element ko React ke hawale karta hai |
.render(...) | App component ko screen par dikhata hai |
StrictMode | Development mein extra checks, production par asar nahi |
npm run build | Optimized files dist/ folder mein |
npm run preview | dist/ build ko locally test karna |
Environment variables .env file mein VITE_ prefix ke saath likhein aur import.meta.env.VITE_API_URL se parhein.
Interview tip
"Create React App aur Vite mein kya farq hai?" CRA webpack par tha aur deprecate ho chuka hai. Vite dev mein native ES modules aur fast HMR deta hai aur production build alag optimized bundler se banata hai.
6. Common mistakes
Galati: Naye project ke liye create-react-app use karna
Why? CRA deprecated hai, outdated dependencies aur warnings deta hai.
Behtar: npm create vite@latest use karein ya full-stack app ke liye Next.js.
Galati: npm install chalaye bina npm run dev chalana
Why? node_modules nahi hota, is liye vite command not found jaisa error aata hai.
Behtar: Project banane ke baad pehle npm install, phir npm run dev.
Galati: Env variable bina VITE_ prefix ke likhna
Why? Vite security ke liye sirf VITE_ wale variables browser code ko deta hai, baqi undefined aate hain.
Behtar: Browser wale variables VITE_ se shuru karein aur secret keys frontend .env mein kabhi na rakhein.
Galati: Purane tutorial se ReactDOM.render copy karna
Why? ReactDOM.render React 19 mein remove ho chuka hai.
Behtar: react-dom/client se createRoot import karein aur root.render use karein.
7. Pehle aur baad mein kya seekhein?
| Stage | Topics |
|---|---|
| Pehle | Introduction, npm & Packages, Modules |
| Saath | JSX |
| Baad mein | Components, Styling in React |
Final quick revision
| Point | Detail |
|---|---|
| Vite | Fast dev server aur build tool |
| Command | npm create vite@latest my-app -- --template react |
| Entry | index.html se src/main.jsx |
| Root API | createRoot(...).render(...) |
| Dev port | localhost:5173 |
| CRA | Deprecated, use na karein |