Vite Build Error: Failed to Resolve Import— Fixed [Complete Guide 2026]
Also covers: missing package · path alias · Node built-ins · dynamic import · CSS import
⚡ The Error You Are Seeing
[vite] Internal server error: Failed to resolve import "lodash" from "src/utils.ts".
Does the file exist?
[vite:resolve] Failed to resolve import "@/components/Button" from "src/App.tsx".
Error: Cannot find module 'path' — browser build cannot use Node.js built-inRead the Vite Error Message Carefully
The Vite error message contains the import string that failed and the file it was imported from. These two pieces of information tell you exactly what to fix.
- A bare specifier like 'lodash' or 'axios' → package not installed
- A path alias like '@/components/Button' → alias not in vite.config.ts
- A Node.js built-in like 'fs', 'path', 'crypto' → browser code cannot use Node modules
- A relative path like '../utils/helper' → file doesn't exist or wrong case
Package Not Installed
Most common — forgot to npm install# Install the missing package
npm install lodash
npm install axios
# If package is in package.json but missing from node_modules:
rm -rf node_modules package-lock.json
npm install
# Verify installation
ls node_modules/lodashIn a monorepo, make sure you install the package in the correct workspace package — the one that imports it, not just the root.
Path Alias Not Configured in Vite
@/ aliases work in TS but not at runtimetsconfig paths don't affect Vite's module resolver. You must also configure aliases in vite.config.ts.
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import path from "path";
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
});
// OR — use vite-tsconfig-paths to auto-sync with tsconfig.json
import tsconfigPaths from "vite-tsconfig-paths";
export default defineConfig({
plugins: [react(), tsconfigPaths()],
});Node.js Built-in Module in Browser Code
fs · path · crypto · os — browser cannot use thesePre-transform error: Failed to resolve import "path" from "src/utils.ts"
Node built-in modules are not available in browser environments.Node.js built-ins cannot run in the browser. You need to either move the code server-side or use browser-compatible alternatives.
// path.join → use URL API or template strings in browser
// Instead of: import path from 'path'; path.join(dir, file)
const url = new URL(file, baseUrl).pathname;
// crypto.randomUUID → available natively in modern browsers
const id = crypto.randomUUID(); // no import needed
// fs → use browser File API or fetch for file operations
const response = await fetch("/api/file-content");
const text = await response.text();
// For Vite SSR or server-side only files, mark them:
// These run only on the server, never in browser builds
if (import.meta.env.SSR) {
const { readFile } = await import("fs/promises");
}import { defineConfig } from "vite";
import { nodePolyfills } from "vite-plugin-node-polyfills";
export default defineConfig({
plugins: [
nodePolyfills({
include: ["path", "crypto", "buffer"],
}),
],
});CSS or Asset Import Failing
Static assets · image imports · CSS modules// CSS — import directly (no quotes around path)
import "./styles/global.css";
import styles from "./Button.module.css"; // CSS modules
// Images — import as URL
import logoUrl from "./assets/logo.png";
// Use in JSX: <img src={logoUrl} />
// SVG — import as React component (with @svgr/rollup plugin)
import { ReactComponent as Logo } from "./logo.svg";
// Raw file content
import shaderCode from "./shader.glsl?raw";
// URL only (no file inlining)
import assetUrl from "./file.bin?url";Vite uses query suffixes (?raw, ?url, ?worker) to control how assets are imported. If a plain import fails, try the appropriate suffix.
Works in Dev but Fails in Production Build
Dynamic imports · optimizeDeps · Rollup strictness// WRONG — Rollup cannot statically analyze variable paths
const module = await import(`./pages/${pageName}`);
// CORRECT — use import.meta.glob for dynamic page loading
const pages = import.meta.glob("./pages/*.tsx");
const module = await pages[`./pages/${pageName}.tsx`]();
// For packages that cause pre-bundling issues:
// vite.config.ts
export default defineConfig({
optimizeDeps: {
include: ["problematic-package"],
exclude: ["package-that-bundles-itself"],
},
});Prevention Checklist
- Run vite build locally before deploying — catches resolution errors before CI
- Use vite-tsconfig-paths plugin to keep aliases in sync with tsconfig
- Never import Node.js built-ins (fs, path) in files that run in the browser
- Use import.meta.glob for dynamic module loading — not template literal imports
- Check for case sensitivity in file paths — Vite on Linux is case-sensitive
Frequently Asked Questions
What causes Vite 'failed to resolve import' error?+−
Vite 'failed to resolve import' works in dev but fails in build — why?+−
How do I import Node.js built-ins like 'path' in a Vite browser project?+−
Vite failed to resolve import for a CSS file — how to fix?+−
How do I fix Vite failed to resolve import for a dynamic import with a variable?+−
Does Vite support CommonJS require() imports?+−
Need Expert Help?
We Build Vite-Powered React Apps
Softplix migrates CRA projects to Vite and builds new React applications with optimal Vite configuration, TypeScript, and CI/CD pipelines.
Talk to an Engineer