Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 2 of 10
Modern Project Setup with Vite
The era of Create React App is over. Vite is the industry standard for new React projects - it's 10-100x faster than CRA due to native ES modules in development and Rollup for production bundling. Getting your project structure right from day one prevents painful refactors later.
Why Vite over Create React App
- CRA uses Webpack which bundles EVERYTHING before serving - cold start can take 30-60s on large projects
- Vite serves source files as native ES modules - the browser does the module resolution; cold start is under 1 second
- Vite uses esbuild (written in Go) for TypeScript/JSX transpilation - 20-100x faster than Babel
- Vite uses Rollup for production builds - smaller bundles, better tree-shaking than Webpack defaults
- CRA is effectively deprecated - the React team no longer recommends it for new projects
- Vite has first-class TypeScript support, CSS Modules, PostCSS, and environment variables built in
Scaffolding & Recommended Folder Structure
# Create a new React + TypeScript project with Vite
npm create vite@latest my-app -- --template react-ts
cd my-app
npm install
npm run dev
# Recommended production-grade folder structure:
src/
โโโ app/ # App-level setup: router, providers, global styles
โ โโโ App.tsx
โ โโโ providers.tsx
โโโ features/ # Feature-based modules (vertical slice architecture)
โ โโโ auth/
โ โ โโโ components/
โ โ โโโ hooks/
โ โ โโโ api/
โ โ โโโ index.ts # Public API of the feature (barrel export)
โ โโโ dashboard/
โโโ components/ # Truly shared, generic UI components (Button, Modal)
โโโ hooks/ # Shared custom hooks (useDebounce, useLocalStorage)
โโโ lib/ # Third-party wrappers, utility functions
โโโ types/ # Global TypeScript types/interfaces
โโโ main.tsx # Entry point
# vite.config.ts - add path aliases for clean imports
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { resolve } from 'path';
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
'@': resolve(__dirname, './src'),
'@features': resolve(__dirname, './src/features'),
'@components': resolve(__dirname, './src/components'),
},
},
});
# tsconfig.json - add paths to match
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["src/*"],
"@features/*": ["src/features/*"],
"@components/*": ["src/components/*"]
}
}
}
# Now you can do clean imports instead of ../../../components/Button
import { Button } from '@components/Button';
import { useAuth } from '@features/auth';Environment Variables & Configuration
# .env.development
VITE_API_URL=http://localhost:3001
VITE_APP_NAME=My App (Dev)
# .env.production
VITE_API_URL=https://api.myapp.com
VITE_APP_NAME=My App
# .env.local (gitignored - for secrets)
VITE_STRIPE_KEY=sk_test_...
# In your code - Vite ONLY exposes vars prefixed with VITE_
const apiUrl = import.meta.env.VITE_API_URL;
const isDev = import.meta.env.DEV; // true in development
const isProd = import.meta.env.PROD; // true in production
const mode = import.meta.env.MODE; // "development" | "production"
// Type-safe env vars - create src/vite-env.d.ts
/// <reference types="vite/client" />
interface ImportMetaEnv {
readonly VITE_API_URL: string;
readonly VITE_APP_NAME: string;
}
interface ImportMeta {
readonly env: ImportMetaEnv;
}Essential Development Dependencies
# Core dependencies
npm install react react-dom
npm install -D typescript @types/react @types/react-dom
# Routing
npm install react-router-dom
# State management
npm install zustand
npm install @tanstack/react-query
# Forms & validation
npm install react-hook-form zod @hookform/resolvers
# Linting & formatting (installed separately - covered in the Husky topic)
npm install -D eslint @eslint/js eslint-plugin-react-hooks
npm install -D prettier eslint-config-prettier
# Testing
npm install -D vitest @testing-library/react @testing-library/user-event jsdom
# package.json scripts
{
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"lint": "eslint src --ext .ts,.tsx",
"format": "prettier --write src",
"test": "vitest",
"test:ui": "vitest --ui",
"type-check": "tsc --noEmit"
}
}Common Mistakes - Project Setup
- Using `.js` extensions instead of `.tsx` for React files - TypeScript catches so many bugs at compile time; always use TypeScript
- Putting everything in `src/components/` - as the project grows this becomes unmanageable; use feature-based vertical slices
- Not setting up path aliases - `../../../components/Button` imports are a maintenance nightmare; configure `@/` aliases immediately
- Committing `.env.local` - never commit files with real secrets; add it to `.gitignore` and use a `.env.example` for documentation
- Skipping TypeScript `strict` mode - always enable `"strict": true` in tsconfig; the temporary pain is worth the long-term safety
Tip
Tip
Practice Modern Project Setup with Vite in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and flow for Modern Project Setup with Vite
Practice Task
Note
Practice Task - (1) Write a working example of Modern Project Setup with Vite from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Modern Project Setup with Vite is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Key Takeaways
- The era of Create React App is over.
- CRA uses Webpack which bundles EVERYTHING before serving - cold start can take 30-60s on large projects
- Vite serves source files as native ES modules - the browser does the module resolution; cold start is under 1 second
- Vite uses esbuild (written in Go) for TypeScript/JSX transpilation - 20-100x faster than Babel
Finished this lesson?