Official documentation: vite.dev
Prerequisites
Before using Vite, ensure Node.js is installed on your system.Check Node.js installation
Installation Methods
- npm (Global)
- npx (Temporary)
Install Vite globally on your system.
Install globally
Global installation allows you to use Vite commands from anywhere, but project-specific versions are recommended for consistency.
Quick Start
Create Project
Use the Vite project creator to scaffold a new application.You’ll be prompted to:
Create Vite project
- Choose a project name
- Select a framework (React, Vue, Svelte, etc.)
- Choose a variant (JavaScript, TypeScript, etc.)
Common Commands
| Command | Description |
|---|---|
node -v | Check if Node.js is installed |
npm | Install packages globally |
npx | Create project temporarily without global install |
npm install -g vite | Install Vite globally |
npm create vite@latest | Create a new Vite project |
npm run dev | Start development server |
npm run build | Build for production |
npm run preview | Preview production build locally |
Project Structure
A typical Vite project structure:Configuration
Basic vite.config.js
Basic vite.config.js
vite.config.js
Path Aliases
Path Aliases
vite.config.js with aliases
Development Workflow
Framework-Specific Setup
- React
- Vue
- Svelte
- Vanilla JS
Create React project
Key Features
Lightning Fast
Instant server start and ultra-fast Hot Module Replacement (HMR) regardless of app size.
Optimized Builds
Production builds use Rollup for highly optimized static assets with automatic code splitting.
Rich Features
TypeScript, JSX, CSS, and more work out of the box with no configuration needed.
Framework Agnostic
First-class support for React, Vue, Svelte, and vanilla JavaScript/TypeScript.
Environment Variables
Vite exposes environment variables throughimport.meta.env.
Using Environment Variables
Using Environment Variables
Create Access in your code:Different environment files:
.env files in your project root:.env
.env- Loaded in all cases.env.local- Loaded in all cases, ignored by git.env.[mode]- Only loaded in specified mode (e.g.,.env.production).env.[mode].local- Only loaded in specified mode, ignored by git
Troubleshooting
Port Already in Use
Port Already in Use
If port 5173 is already in use, Vite will automatically try the next available port. To specify a custom port:Or run with CLI flag:
vite.config.js
Module Not Found Errors
Module Not Found Errors
Ensure all dependencies are installed:Clear cache and reinstall:
Slow HMR Performance
Slow HMR Performance
If HMR becomes slow in large projects:
- Reduce the number of files being watched
- Use path aliases to optimize imports
- Check for circular dependencies
- Consider splitting large components
Best Practices
Use npx
Always use
npm create vite@latest to ensure you’re using the latest version.Environment Variables
Prefix environment variables with
VITE_ and never commit .env files with secrets.Path Aliases
Configure path aliases to avoid deep relative imports like
../../../components/Button.Build Analysis
Regularly analyze your production bundle size to catch bloat early.