Vite
Vite is a next-generation frontend build tool, currently at stable version 8.0.8, designed to significantly improve the frontend development experience. It functions as a dev server serving source files over native ES modules with fast Hot Module Replacement (HMR), and a build command that bundles code with Rollup for optimized production assets. Vite provides rich built-in features, a universal plugin interface, and fully typed APIs, making it highly extensible. Patch releases are frequent, with major versions released periodically.
Common errors
-
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ESM syntax (like `import` and `export default`) in a Vite configuration file that is being treated as CommonJS.fixEnsure your `vite.config.js` file is interpreted as an ES Module. This can be done by adding `"type": "module"` to your `package.json` or by renaming `vite.config.js` to `vite.config.mjs`. -
Your current Node.js version is [X]. Vite requires Node.js version ^20.19.0 || >=22.12.0.
cause The Node.js version running Vite does not meet the minimum requirements specified in the package's `engines` field.fixUpgrade your Node.js installation to version `^20.19.0` or `>=22.12.0`. -
ReferenceError: process is not defined
cause Accessing `process.env` directly in client-side code, which is a Node.js global and not available in the browser environment.fixUse `import.meta.env` for environment variables in client-side code. For custom variables, ensure they are prefixed with `VITE_` (e.g., `VITE_APP_API_URL`). -
Failed to resolve import "some-package" from "src/main.ts"
cause The imported module cannot be found in `node_modules`, its `package.json` `exports` field is misconfigured, or the import path is incorrect.fixVerify that `some-package` is installed correctly via `npm install some-package`. Check the module's documentation for correct import paths and ensure its `package.json` properly defines its exports for ESM environments. -
RollupError: "SymbolName" is not exported by "module-name.js"
cause Attempting to import a named export that does not exist in the specified module, often due to incorrect CommonJS/ESM interop or misconfigured exports in a third-party library.fixCheck the module's documentation or source code for available exports. Ensure named exports are correctly defined or consider using a default import if the module primarily uses default exports.
Warnings
- breaking Vite requires Node.js version `^20.19.0` or `>=22.12.0`.
- gotcha Vite configuration files (e.g., `vite.config.js`) must be written as native ES Modules (ESM).
- gotcha Environment variables exposed to client-side code must be prefixed with `VITE_`.
- gotcha The `esbuild` package is a peer dependency and is crucial for Vite's fast cold start and build performance. While Vite can fall back without it, it's highly recommended.
- gotcha For supporting older browsers (e.g., IE11), the `@vitejs/plugin-legacy` plugin is required.
Install
-
npm install vite -
yarn add vite -
pnpm add vite
Imports
- defineConfig
import { defineConfig } from 'vite'
Quickstart
npm create vite@latest my-vite-app -- --template react-ts cd my-vite-app npm install npm run dev