API Reference

Vite

Next-generation build tool that serves native ES modules during development and bundles with Rollup for optimized production output.

Official Documentation ↗
CompatibilityThis documentation covers version v8.3.5. Check the official docs for the latest API surface.

Installation

npmnpm install --save-dev vite
pnpmpnpm add -D vite

Methods

Overview

Vite is a build tool that serves native ES modules in development and bundles production code with Rollup, giving fast startup and optimized output. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • createServer — Creates a dev server instance with HMR that you can start, inspect, or close programmatically.
  • build — Runs a production build and resolves with the underlying Rollup output metadata.
  • preview — Serves the production build locally for a final smoke check before deploy.

Example

import { createServer } from 'vite';

const server = await createServer({ server: { port: 5173 } });
await server.listen();
server.printUrls();

Notes

The dev and build pipelines differ intentionally, so verify behavior against the production build. Only VITE_-prefixed environment variables reach the client bundle, which limits accidental secret exposure.

Verification

Run createServer for a dev smoke test and build then preview for production, confirming both serve the same routes without console errors.

FAQ

How do I expose the dev server on the local network?
Set server.host to "0.0.0.0" or true in the config (or pass --host to the CLI) so other devices can reach it.
Why does Vite behave differently in dev and build?
Dev uses native ESM and esbuild transforms for speed, while build uses Rollup for bundling and tree-shaking, so some conditions like import.meta.env differ.
How do I load environment variables?
Define them in .env files with a VITE_ prefix; import.meta.env exposes them to client code, and only VITE_-prefixed keys are inlined into the bundle.