EveryPage

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.env object automatically. Variables prefixed with VITE_ are exposed in client-side source code after bundling, while unprefixed ones (like DB_PASSWORD) are not, so import.meta.env.DB_PASSWORD is undefined. If you want a different prefix, see the envPrefix option.

    Defining variables in .env files. Vite loads additional variables from files in your environment directory:

    • .env is loaded in all cases.
    • .env.local is loaded in all cases and ignored by git.
    • .env.[mode] is loaded only in the specified mode.
    • .env.[mode].local is loaded only in the specified mode and ignored by git.

    You should add *.local to your .gitignore so 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 .env or .env.local are 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 .env files. The files are loaded at Vite's start, so restart the server after changing them.

    Modes. The dev server runs in development mode and vite build runs in production mode by default, so vite build loads .env.production if it exists. You can override the mode with --mode, for example vite build --mode staging, and then create a .env.staging file. NODE_ENV and 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 a vite-env.d.ts in src and augment ImportMetaEnv. That file must not contain any import statements, or the augmentation won't work.

    In HTML. Properties of import.meta.env can 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 becoming undefined as 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 .env files within envDir and merges them with matching variables already in process.env. By default it only loads variables prefixed with VITE_, unless prefixes is 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 build command. It uses <root>/index.html as 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.rolldownOptions for 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 --watch enables the Rolldown watcher so that changes to bundled files trigger a rebuild, while build.watch sets the underlying watcher options. Changes to the config and its dependencies still require restarting the build command.
    • Environments: Running vite build and vite build --ssr builds only the client and SSR environments respectively, for backward compatibility. When the builder option is set (even to {}, as vite build --app does), vite build builds the entire app instead, with environments built in series in the order of the environments record.

    For server-side rendering projects, the production build requires a client build and a separate SSR build using the --ssr flag 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 ​

  • How do I serve static assets from the public folder?

    Place the asset in a special public directory 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, so public/icon.png is 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 the publicDir option. 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