Development Server
Start the Vite development server with hot module replacement (HMR):http://localhost:5173 by default.
Dev Server Features
- Hot Module Replacement (HMR): Changes appear instantly without full page reload
- Fast Startup: Vite uses native ES modules for lightning-fast cold starts
- Optimized Dependencies: Pre-bundled with esbuild for performance
- Error Overlay: Clear error messages displayed in the browser
Available Scripts
All npm scripts are defined inpackage.json:
Development
- Watches for file changes
- Recompiles modified modules
- Updates the browser without full reload
- Preserves application state when possible
Production Build
- Type Checks: Runs
tsc -bto validate TypeScript types - Builds: Runs
vite buildto create optimized production bundle - Output: Generates files in the
dist/directory
The build will fail if there are any TypeScript type errors. Fix all type errors before deploying.
Preview Production Build
- Serves the built files from
dist/ - Runs on a local server (default:
http://localhost:4173) - Simulates production environment
Linting
- TypeScript type usage
- React Hooks rules
- React Fast Refresh compatibility
- Code style consistency
Hot Module Replacement (HMR)
Vite provides fast and reliable HMR out of the box:How HMR Works
- File Change Detection: Vite watches for changes in your source files
- Module Graph Update: Only the changed module and its dependencies are updated
- State Preservation: React Fast Refresh preserves component state when possible
- Instant Updates: Changes appear in milliseconds
HMR with React
The@vitejs/plugin-react enables React Fast Refresh:
When HMR Triggers Full Reload
Some changes require a full page reload:- Changes to
index.html - Changes to Vite configuration files
- Changes to environment variables
- Syntax errors in modules
Build Process
The production build process optimizes your application for deployment.Build Command Breakdown
TypeScript Type Checking
tsc -b performs incremental type checking across project references:- Checks
tsconfig.app.json(application code) - Checks
tsconfig.node.json(Vite config) - Uses cached build info from
node_modules/.tmp/ - Fails the build if type errors are found
Vite Build
vite build creates the production bundle:- Bundles JavaScript with Rollup
- Processes CSS with PostCSS and TailwindCSS
- Optimizes assets (images, fonts, etc.)
- Generates hashed filenames for cache busting
- Creates source maps for debugging
Build Output Structure
Build Optimizations
Vite automatically applies production optimizations:| Optimization | Description |
|---|---|
| Code Splitting | Splits code into smaller chunks for faster loading |
| Tree Shaking | Removes unused code from the bundle |
| Minification | Compresses JavaScript and CSS |
| Asset Hashing | Adds content hashes to filenames for caching |
| CSS Extraction | Extracts CSS into separate files |
| Source Maps | Generates source maps for debugging |
Development Workflow
Typical Development Cycle
Make Changes
Edit React components in
src/:- Changes appear instantly via HMR
- Component state is preserved
- Console shows any errors
Best Practices
Keep Dev Server Running
Keep Dev Server Running
Leave
npm run dev running while developing for instant feedback. HMR works best when the dev server stays active.Fix Type Errors Early
Fix Type Errors Early
TypeScript errors will prevent production builds. Use your editor’s TypeScript integration to catch errors as you code.
Run Linter Regularly
Run Linter Regularly
Run
npm run lint before committing code. This catches common React and TypeScript issues early.Test Production Builds
Test Production Builds
Always run
npm run build && npm run preview before deploying to catch production-only issues.Performance Tips
Faster Development
- Use Chrome DevTools: Enable React DevTools extension for better debugging
- Leverage HMR: Avoid full page reloads by keeping component state pure
- Code Splitting: Use dynamic imports for large dependencies
Faster Builds
- Reduce Dependencies: Remove unused npm packages
- Optimize Images: Compress images before adding to
public/ - Use Production Mode:
NODE_ENV=productionenables optimizations
Troubleshooting
Dev Server Won’t Start
- Port Conflict
- Permission Issues
- Cache Issues
If port 5173 is in use, specify a different port:
HMR Not Working
- Check Browser Console: Look for HMR connection errors
- Verify File Watcher: Ensure your OS supports file watching
- Check Import Paths: Incorrect imports can break HMR
- Restart Dev Server: Sometimes a restart fixes the issue
Build Failures
TypeScript Errors
TypeScript Errors
- Missing type definitions
- Incorrect prop types
- Unused variables (with strict mode enabled)
Out of Memory
Out of Memory
For large projects, increase Node memory:
Module Resolution Errors
Module Resolution Errors
Ensure all imports are correct:
- Check file extensions in imports
- Verify package is installed
- Clear node_modules and reinstall
Next Steps
Configuration
Customize Vite, TypeScript, ESLint, and TailwindCSS settings
Setup
Review installation and dependency requirements