Diagnosing Slow Vite Dev Server Startup Due to Dependency Pre‑bundling Bottlenecks
A step‑by‑step diagnostic guide for when Vite’s dev server stalls on 'Optimizing dependencies…' due to Node version, esbuild, dependency count, or native addons.
28 Jun 2026, 12:13 UTC

Recognizable condition
When you run npm run dev (or vite) the development server hangs for more than ~10 seconds on the message "Optimizing dependencies…" before becoming ready. The console may show a long pause with no further output, and the final startup time reported by Vite exceeds the expected sub‑second range.
Cause / diagnostic table
| Potential cause | What to look for in logs or environment |
|---|---|
| Node version <14 | Vite’s bundled esbuild requires Node ≥14; older versions may cause esbuild to fall back to a slower path or fail silently. |
| Missing or outdated esbuild binary | The esbuild command reports a version older than the one Vite expects, or npx esbuild -v returns "command not found". |
| Very large dependency count (>200 packages) | npm list --depth=0 shows many entries; pre‑bundling work scales roughly linearly with the number of distinct packages. |
| Dependencies with native addons | Packages that contain compiled binaries (e.g., sharp, canvas, bcrypt) cause esbuild to spend extra time parsing and transforming native‑code shims. |
Ordered checks
- Verify Node version
Run
node -vin the project root. The output should be v14.x, v16.x, v18.x, or newer. If you see v12 or earlier, upgrade Node.# Example (run in terminal, requires user permission to install Node) node -v # Expected: v18.17.0 or similar - Confirm esbuild presence and version
Execute
npx esbuild -v. Compare the printed version with the esbuild version bundled by your Vite release (see Vite changelog orvite --versionoutput).npx esbuild -v # Example output: 0.19.12 - List top‑level dependencies
Run
npm list --depth=0and count the lines. Note if the count exceeds ~200.npm list --depth=0 # Example output shows 237 packages - Inspect Vite startup timing
Watch the console while starting the dev server. Look for a line like:
Optimizing dependencies… (took 8.3s)If the elapsed time is high, proceed to the next check.
- Try a clean reinstall
Remove the node modules and lockfile, then reinstall using a clean‑install command to eliminate possible corruption.
# Using npm (requires write permission to the project directory) rm -rf node_modules package-lock-json npm ci # or with yarn: yarn install --frozen-lockfile
Fixes tied to findings
- Node version too low
- Upgrade to an active LTS release (e.g., Node 18 or 20) using your version manager (
nvm install 18,fnm use 20, or the official installer). - After upgrading, repeat
node -vand restart the dev server.
- Upgrade to an active LTS release (e.g., Node 18 or 20) using your version manager (
- Esbuild missing/outdated
- Reinstall project dependencies (
npm cioryarn install --frozen-lockfile) to fetch the esbuild binary bundled with Vite. - If you deliberately use a custom esbuild version, ensure it matches the version Vite expects (see Vite docs for your version).
- Reinstall project dependencies (
- High dependency count
- Exclude large libraries from pre‑bundling via
optimizeDeps.exclude(Vite 2) oroptimizeDeps.entrieswith a filter function (Vite 3+). Example for Vite 3:
// vite.config.js import { defineConfig } from 'vite' export default defineConfig({ optimizeDeps: { entries: ['src/main.js'], filter: (dep) => { // Skip heavy utility lodash‑like libs return !/^lodash-es$/.test(dep) } } }) - Exclude large libraries from pre‑bundling via
- Alternatively, increase worker concurrency:
optimizeDeps: {
maxWorkers: require('os').cpus().length
}
- Force pre‑bundling of the problematic package so esbuild processes it once:
optimizeDeps: {
include: ['sharp']
}
ssr: {
external: ['sharp']
}
Escalation criteria
If after applying the relevant fixes the dev server still takes >30 seconds to become ready:
- Consider replacing the default esbuild pre‑bundler with a custom plugin that allows higher concurrency (e.g.,
@vitejs/plugin-esbuildwithnumWorkersoption) or a different implementation. - For projects heavily reliant on native addons or large monorepos, evaluate whether a different bundler (such as webpack with its thread‑loader) yields better incremental build times.
- Before switching, profile the build with
--debugorvite --mode debugto confirm where time is spent.
Practical verification
- Measure startup time before and after changes using a timing wrapper:
# Linux/macOS
time npm run dev
# PowerShell on Windows
Measure-Command { npm run dev }
- Look for the line that reports the optimization duration, e.g.,
Optimizing dependencies… (took 2.1s). The number should be noticeably lower than the baseline. - Optionally, inspect the
node_modules/.vitefolder: a large number of chunks or unusually large file sizes indicate heavy pre‑bundling work; after optimization, the total size should shrink.
Limitations
- The
optimizeDepsAPI changed between Vite 2 (exclude/include) and Vite 3+ (entries/filter). Ensure you consult the documentation that matches your installed Vite version (npm list vite). - Increasing
maxWorkersraises CPU and memory usage during startup; on CI agents or low‑resource laptops this can cause swapping or OOM kills. Monitor system metrics (e.g.,topor Task Manager) while testing. - Frequent manual deletion of
node_modulesand lockfiles can lead to drift if you are not using a lockfile‑preserving install command. Prefernpm ci,yarn install --frozen-lockfile, orpnpm install --frozen-lockfilefor reproducibility.
By following the ordered checks and applying the targeted fixes above, you should be able to reduce Vite dev server startup time caused by dependency pre‑bundling bottlenecks. If the problem persists, move to the escalation steps and consider alternative tooling.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.