Skip to main content

Development Server

Start the Vite development server with hot module replacement (HMR):
npm run dev
The development server will start on 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 in package.json:

Development

npm run dev
Starts the Vite development server with HMR enabled. The server automatically:
  • Watches for file changes
  • Recompiles modified modules
  • Updates the browser without full reload
  • Preserves application state when possible

Production Build

npm run build
Builds the application for production. This command:
  1. Type Checks: Runs tsc -b to validate TypeScript types
  2. Builds: Runs vite build to create optimized production bundle
  3. 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

npm run preview
Locally preview the production build before deploying. This:
  • Serves the built files from dist/
  • Runs on a local server (default: http://localhost:4173)
  • Simulates production environment
The preview server is not designed for production use. Use proper hosting services for deployment.

Linting

npm run lint
Runs ESLint to check code quality and enforce standards. The linter checks:
  • 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

  1. File Change Detection: Vite watches for changes in your source files
  2. Module Graph Update: Only the changed module and its dependencies are updated
  3. State Preservation: React Fast Refresh preserves component state when possible
  4. Instant Updates: Changes appear in milliseconds

HMR with React

The @vitejs/plugin-react enables React Fast Refresh:
// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

export default defineConfig({
  plugins: [react()],
})

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

tsc -b && vite build
1

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
2

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
3

Output Generation

Build artifacts are output to dist/:
  • index.html - Entry point
  • assets/*.js - JavaScript bundles
  • assets/*.css - Compiled stylesheets
  • Static assets copied from public/

Build Output Structure

dist/
├── index.html
├── assets/
│   ├── index-[hash].js
│   ├── index-[hash].css
│   └── [other-assets]
└── [public-files]

Build Optimizations

Vite automatically applies production optimizations:
OptimizationDescription
Code SplittingSplits code into smaller chunks for faster loading
Tree ShakingRemoves unused code from the bundle
MinificationCompresses JavaScript and CSS
Asset HashingAdds content hashes to filenames for caching
CSS ExtractionExtracts CSS into separate files
Source MapsGenerates source maps for debugging

Development Workflow

Typical Development Cycle

1

Start Dev Server

npm run dev
Open browser to http://localhost:5173
2

Make Changes

Edit React components in src/:
  • Changes appear instantly via HMR
  • Component state is preserved
  • Console shows any errors
3

Check Code Quality

npm run lint
Fix any linting errors or warnings
4

Build for Production

npm run build
Ensure TypeScript types are valid and build succeeds
5

Preview Build

npm run preview
Test the production build locally

Best Practices

Leave npm run dev running while developing for instant feedback. HMR works best when the dev server stays active.
TypeScript errors will prevent production builds. Use your editor’s TypeScript integration to catch errors as you code.
Run npm run lint before committing code. This catches common React and TypeScript issues early.
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
// Lazy load heavy components
const HeavyComponent = lazy(() => import('./HeavyComponent'))

Faster Builds

  • Reduce Dependencies: Remove unused npm packages
  • Optimize Images: Compress images before adding to public/
  • Use Production Mode: NODE_ENV=production enables optimizations

Troubleshooting

Dev Server Won’t Start

If port 5173 is in use, specify a different port:
npm run dev -- --port 3000

HMR Not Working

  1. Check Browser Console: Look for HMR connection errors
  2. Verify File Watcher: Ensure your OS supports file watching
  3. Check Import Paths: Incorrect imports can break HMR
  4. Restart Dev Server: Sometimes a restart fixes the issue

Build Failures

# See detailed type errors
npx tsc --noEmit
Fix all type errors before building. Common issues:
  • Missing type definitions
  • Incorrect prop types
  • Unused variables (with strict mode enabled)
For large projects, increase Node memory:
NODE_OPTIONS="--max-old-space-size=4096" npm run build
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

Build docs developers (and LLMs) love