INSTALLATION_GUIDE.md
md6 kBIn Examples.zip
⚡ Installation Guide - React with Vite
This guide will help you set up and run the React code examples built with Vite.
📋 Prerequisites
Before you begin, make sure you have the following installed:
Node.js and npm
- Node.js: Version 14.18+ or 16+ (Latest LTS recommended)
- npm: Version 6+ (comes with Node.js)
Check your versions:
node --version
npm --versionDownload Node.js: If you don't have Node.js installed, download it from nodejs.org
🚀 Quick Start (Single Chapter)
Step 1: Navigate to a Chapter
cd React_Code_Examples/Chapter01-IntroductionStep 2: Install Dependencies
npm installThis will install:
- React (^18.2.0)
- React-DOM (^18.2.0)
- Vite (^5.0.8)
- @vitejs/plugin-react (^4.2.1)
Step 3: Run the Development Server
npm run devThe app will be available at:
- http://localhost:3000 (configured in examples)
- or http://localhost:5173 (Vite's default)
Step 4: Make Changes
Try editing src/App.jsx - your changes will appear instantly thanks to Vite's HMR!
📦 Running All Chapters
If you want to install dependencies for all chapters at once:
Windows (PowerShell):
Get-ChildItem -Directory -Filter "Chapter*" | ForEach-Object {
Write-Host "Installing $($_.Name)..." -ForegroundColor Green
Set-Location $_.FullName
npm install
Set-Location ..
}
Mac/Linux (Bash):
for dir in Chapter*/; do
echo "Installing $dir..."
cd "$dir"
npm install
cd ..
done🎯 Available Scripts
Each chapter supports these npm scripts:
npm run dev
Starts the Vite development server with:
- ⚡ Instant server start
- 🔥 Lightning-fast HMR
- 📝 Error overlay in browser
npm run build
Builds the app for production:
- Optimized bundles
- Minified code
- Tree-shaking
- Output in
dist/folder
npm run preview
Preview the production build locally before deploying
🌟 Vite Features
Hot Module Replacement (HMR)
- Changes appear in < 100ms
- State is preserved during updates
- No full page reload needed
Fast Server Start
- Vite starts instantly - no bundling in development
- Uses native ES modules
Optimized Builds
- Rollup-based production builds
- Automatic code splitting
- Tree-shaking
🔧 Configuration
Each chapter includes a vite.config.js:
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
server: {
port: 3000, // Custom port
open: true // Auto-open browser
}
})Common Customizations:
Change port:
server: {
port: 5173
}Disable auto-open:
server: {
open: false
}📁 Project Structure
ChapterXX-Topic/
├── index.html # Entry HTML (at root!)
├── package.json # Dependencies and scripts
├── vite.config.js # Vite configuration
├── public/ # Static assets
└── src/
├── main.jsx # Application entry point
├── App.jsx # Main component
├── App.css # Component styles
└── index.css # Global styles
Key Differences from Create React App:
index.htmlis at root (not in public/)- Entry file is
main.jsx(not index.js) - Use
.jsxextension for JSX files - Command is
npm run dev(not npm start)
🐛 Troubleshooting
Port Already in Use
Problem: Port 3000 or 5173 is already in use
Solution:
- Kill the process using the port
- Or change the port in
vite.config.js:
server: {
port: 3001
}Dependencies Not Installing
Problem: npm install fails
Solution:
# Clear npm cache
npm cache clean --force
# Delete node_modules and package-lock.json
rm -rf node_modules package-lock.json
# Reinstall
npm installVite Not Found
Problem: npm run dev says vite command not found
Solution:
# Make sure you're in the chapter directory
cd Chapter01-Introduction
# Install dependencies
npm install
# Try again
npm run devModule Not Found Errors
Problem: Import errors or module not found
Solution:
- Check file extensions (.jsx vs .js)
- Ensure imports use correct paths
- Reinstall dependencies
🔄 Creating a New Vite React Project
Want to create your own React project with Vite?
# Using npm
npm create vite@latest my-app -- --template react
# Navigate and install
cd my-app
npm install
# Start development
npm run dev📚 Additional Dependencies
React Router (Chapter 10)
npm install react-router-domStyled Components (Chapter 11)
npm install styled-components🌐 Browser Support
Vite targets browsers with native ES modules support:
- Chrome >=87
- Firefox >=78
- Safari >=14
- Edge >=88
For production builds, legacy browsers are supported through plugins.
📖 Learn More
- Vite Documentation: https://vitejs.dev
- React Documentation: https://react.dev
- Vite + React Guide: https://vitejs.dev/guide/#scaffolding-your-first-vite-project
✅ Verification
To verify everything is working:
- Navigate to any chapter
- Run
npm install - Run
npm run dev - Open browser to the provided URL
- You should see the chapter's examples
- Try editing
src/App.jsx- changes should appear instantly!
💡 Tips
- Fast Refresh: Vite's HMR preserves component state
- Console: Check browser console for helpful info
- DevTools: Install React DevTools browser extension
- Hot Reload: Save any file to see instant updates
- Error Overlay: Build errors appear directly in browser
🆘 Need Help?
- Check the browser console for errors
- Verify Node.js and npm versions
- Try deleting
node_modulesand reinstalling - Check that you're using correct commands (
npm run devnotnpm start) - Ensure you're in the correct directory
Happy Learning with Vite! ⚡⚛️