Speed Up Vite Dev Server Startup with optimizeDeps Pre-bundling
Vite's optimizeDeps pre-bundles heavy npm dependencies with esbuild before the dev server starts, turning multi-second cold starts into sub-second readiness. A minimal config change and a quick verification routine show the speed gain.
22 Aug 2025, 21:09 UTC

The Cold-Start Problem
You pull in a few heavy npm packages — maybe lodash, date-fns, or a UI component library — and suddenly vite takes ten seconds to serve the first page. Every file change triggers a cascade of module resolution that stalls hot-module replacement. The culprit isn't Vite itself; it's the sheer number of small files the browser must request before it can render anything.
How optimizeDeps Changes the Game
Vite's optimizeDeps feature hands those heavy dependencies to esbuild before the dev server starts. Esbuild bundles each dependency (and its transitive imports) into a single, cache-friendly JavaScript file. The browser then fetches one pre-bundled chunk instead of dozens of individual modules. Subsequent starts read the cached bundle from .vite/deps, turning a ten-second wait into sub-second readiness.
Minimal Configuration
Most projects need nothing more than the default behavior. Create or edit vite.config.ts at your project root:
import { defineConfig } from 'vite'
export default defineConfig({
optimizeDeps: {
// default: true — auto-detects heavy imports
// include: ['lodash', 'date-fns'] // optional explicit list
// exclude: ['some-native-esm-pkg'] // opt out problematic packages
}
})
Run the dev server from your project directory with npm run dev (or npx vite). No elevated permissions are required; the command writes to .vite/deps inside your project folder.
Worked Example: Adding lodash
Suppose you import import _ from 'lodash' in a fresh Vite + React project. Without pre-bundling, the browser requests ~600 lodash modules. With optimizeDeps enabled (the default), the first vite invocation runs esbuild and writes .vite/deps/lodash.js (and a matching .map file). You can verify the bundle exists:
ls -la .vite/deps/lodash*
Expected output shows a single .js file (~70 KB minified) and its source map. Cold-start time drops from several seconds to under one second on typical hardware. Hot-module reloads for your own components remain instantaneous because only your code is re-transformed.
Trade-offs and Gotchas
- First run penalty: The initial
vitecommand spends extra time running esbuild. On CI or fresh checkouts this adds a few seconds — usually acceptable. - Native ESM packages: Libraries that ship only ES modules and use dynamic
import()or top-levelawaitmay break. Add them toexcludeor setesbuildOptions: { loader: { '.js': 'jsx' } }if they need transformation. - Cache invalidation: After upgrading a dependency version, delete
.vite/deps(or runvite --force) so esbuild rebuilds the bundle. - Node version: Requires Node ≥ 14. Older environments may need a polyfill for
crypto.getRandomValuesused by esbuild's hashing.
Verify It Works
- Time the cold start:
time npx vite(Linux/macOS) orMeasure-Command { npx vite }(PowerShell). Note therealelapsed seconds. - Add a heavy import, restart, and time again. The second run should be noticeably faster.
- Inspect
.vite/deps— each entry corresponds to a top-level dependency you actually import. - Confirm HMR: edit a component file and watch the browser update without full reload.
If startup doesn't improve, check that the dependency isn't listed in optimizeDeps.exclude and that it's a CommonJS or UMD package (esbuild handles those best). For persistent issues, run vite --debug to see which modules esbuild bundles.