Unofficial example
Ask the Vite docs
The Ask this site box, reading vite.dev/guide. Type a question and it answers from those pages, with links.
Loading the box…
An unofficial example. Ask this site is not affiliated with or endorsed by the Vite project. The Vite docs belong to their authors and are read here under their MIT licence.
Questions it answered
Real answers from this box.
How do I use environment variables?
Vite exposes env variables as strings on the
import.meta.envobject automatically. Variables prefixed withVITE_are exposed in client-side source code after bundling, while unprefixed ones (likeDB_PASSWORD) are not, soimport.meta.env.DB_PASSWORDisundefined. If you want a different prefix, see theenvPrefixoption.Defining variables in
.envfiles. Vite loads additional variables from files in your environment directory:.envis loaded in all cases..env.localis loaded in all cases and ignored by git..env.[mode]is loaded only in the specified mode..env.[mode].localis loaded only in the specified mode and ignored by git.
You should add
*.localto your.gitignoreso these local-only files, which can contain sensitive variables, aren't checked into git.Priority. Mode-specific files take precedence over generic ones, but variables defined only in
.envor.env.localare still available. Env variables that already exist when Vite is executed (e.g.VITE_SOME_KEY=123 vite build) have the highest priority and are not overwritten by.envfiles. The files are loaded at Vite's start, so restart the server after changing them.Modes. The dev server runs in
developmentmode andvite buildruns inproductionmode by default, sovite buildloads.env.productionif it exists. You can override the mode with--mode, for examplevite build --mode staging, and then create a.env.stagingfile.NODE_ENVand modes are two different concepts.Types and parsing. Values are always strings, even for numbers or booleans, so convert them to the type you need. For TypeScript IntelliSense on your
VITE_-prefixed variables, create avite-env.d.tsinsrcand augmentImportMetaEnv. That file must not contain anyimportstatements, or the augmentation won't work.In HTML. Properties of
import.meta.envcan be used in HTML files with the%CONST_NAME%syntax, such as%MODE%or%VITE_API_URL%. If the variable doesn't exist, it is left unreplaced rather than becomingundefinedas in JS.Security.
VITE_*variables are bundled into your source code at build time, so they should not contain sensitive information such as API keys. For production secrets, consider a backend server or serverless/edge functions.In Vite config. To load env files programmatically,
loadEnv(mode, envDir, prefixes)loads.envfiles withinenvDirand merges them with matching variables already inprocess.env. By default it only loads variables prefixed withVITE_, unlessprefixesis changed.Answered from Env Variables and Modes — Env Variables , Env Variables and Modes — .env Files , Env Variables and Modes — Modes , Env Variables and Modes — NODE_ENV and Modes , Env Variables and Modes — IntelliSense for TypeScript , Env Variables and Modes — HTML Constant Replacement , JavaScript API — loadEnv
How do I build for production?
To build for production, run the
vite buildcommand. It uses<root>/index.htmlas the build entry point by default and produces an application bundle suitable for serving over a static hosting service.Build behaviour can be adjusted in a few ways:
- Build options: The build can be customized through build config options, including
build.rolldownOptionsfor the underlying Rolldown options. - Chunk splitting: Chunks are configured with
build.rolldownOptions.output.codeSplitting, and framework users should refer to their framework's documentation for this. - Watch mode: Running
vite build --watchenables the Rolldown watcher so that changes to bundled files trigger a rebuild, whilebuild.watchsets the underlying watcher options. Changes to the config and its dependencies still require restarting the build command. - Environments: Running
vite buildandvite build --ssrbuilds only the client and SSR environments respectively, for backward compatibility. When thebuilderoption is set (even to{}, asvite build --appdoes),vite buildbuilds the entire app instead, with environments built in series in the order of theenvironmentsrecord.
For server-side rendering projects, the production build requires a client build and a separate SSR build using the
--ssrflag with an SSR entry.Answered from Building for Production , Building for Production — Customizing the Build , Building for Production — Chunking Strategy , Building for Production — Rebuild on Files Changes , Environment API for Frameworks — Environments During Build , Server-Side Rendering (SSR) — Building for Production
- Build options: The build can be customized through build config options, including
How do I serve static assets from the public folder?
Place the asset in a special
publicdirectory under your project root. Assets there are served at the root path/during development and copied to the root of the dist directory as-is. Reference them in source code using a root absolute path, sopublic/icon.pngis referenced as/icon.png.The public directory suits assets that are never referenced in source code (such as
robots.txt), that must keep their exact file name without hashing, or that you don't want to import just to get their URL. The directory defaults to<root>/public, and you can change it with thepublicDiroption. In general, importing assets is preferred unless you specifically need the guarantees the public directory provides.Answered from Static Asset Handling — The public Directory
Add this to your own docs
Paste your docs address to try it, free and without signing up. Then it is one line of code.
DocusaurusGitBookMintlifyWordPressReadMeMkDocsVitePressStarlightSphinxHugoJekyllNext.jsGhostWebflow