Vite Config Basics
Vite works without configuration, but sooner or later you will want to add a plugin, change the port, or set a base path. All of that goes into a config file in the project root.
The config file
Vite looks for one of these, in order: vite.config.js, vite.config.mjs, vite.config.ts, vite.config.cjs, vite.config.mts, vite.config.cts.
// vite.config.js
import { defineConfig } from 'vite';
export default defineConfig({
server: { port: 3000 },
build: { outDir: 'build' },
});defineConfig does nothing at runtime — it just returns its argument — but it gives your editor full type information, so you get autocompletion and inline docs even in a .js file.
This repository's own config is a good real-world example:
// vite.config.ts (from the ExcelTAD project)
import { defineConfig } from 'vite';
// base './' keeps the build relocatable: dist/ works from any sub-path or even file:// hosting.
export default defineConfig({
base: './',
build: {
target: 'es2022',
chunkSizeWarningLimit: 1500,
},
});The most used options
| Option | Default | Purpose |
|---|---|---|
root |
process.cwd() |
Folder containing index.html |
base |
'/' |
Public base path when deployed |
publicDir |
'public' |
Static files copied as-is |
plugins |
[] |
Array of plugins |
resolve.alias |
— | Path shortcuts like @ → src |
define |
— | Global constant replacements |
css |
— | CSS Modules, pre-processor and PostCSS options |
server |
— | Dev server: port, host, open, proxy |
preview |
— | Same for vite preview |
build |
— | outDir, target, sourcemap, minify, rolldownOptions |
envPrefix |
'VITE_' |
Which env vars are exposed to the client |
Conditional config
Export a function instead of an object to adapt to the command or mode. Vite calls it with { command, mode, isSsrBuild, isPreview }.
import { defineConfig } from 'vite';
export default defineConfig(({ command, mode }) => {
if (command === 'serve') {
return { server: { open: true } };
}
return {
build: { sourcemap: mode === 'staging' },
};
});Here is the same logic as a plain function you can run, showing which config each command gets:
const defineConfig = (c) => c; // the real one is just as simple at runtime
const config = defineConfig(({ command, mode }) => ({
base: command === 'build' ? '/app/' : '/',
server: { port: 5173, open: command === 'serve' },
build: { sourcemap: mode !== 'production', minify: mode === 'production' },
}));
const runs = [
{ command: 'serve', mode: 'development' },
{ command: 'build', mode: 'production' },
{ command: 'build', mode: 'staging' },
];
for (const env of runs) {
const c = config(env);
console.log(`${env.command}/${env.mode}:`, c.base, c.server.open, c.build.sourcemap, c.build.minify);
}serve/development: / true true false
build/production: /app/ false false true
build/staging: /app/ false true false
The function can also be async, for example to read a file or fetch data before returning the config.
How options are merged
Plugins and framework tools often provide part of the config and let you add the rest. Vite exports mergeConfig for that — objects are merged deeply, arrays are concatenated. A simplified version:
function mergeConfig(base, extra) {
const out = { ...base };
for (const [key, value] of Object.entries(extra)) {
if (Array.isArray(value) && Array.isArray(base[key])) out[key] = [...base[key], ...value];
else if (value && typeof value === 'object' && base[key] && typeof base[key] === 'object')
out[key] = mergeConfig(base[key], value);
else out[key] = value;
}
return out;
}
const shared = { plugins: ['react'], server: { port: 5173 }, build: { outDir: 'dist' } };
const local = { plugins: ['inspect'], server: { open: true } };
console.log(mergeConfig(shared, local));{"plugins":["react","inspect"],"server":{"port":5173,"open":true},"build":{"outDir":"dist"}}
define: compile-time constants
define replaces identifiers with values in your code at build time. Values must be valid JavaScript expressions, so wrap strings in JSON.stringify:
export default defineConfig({
define: {
__APP_VERSION__: JSON.stringify('2.1.0'),
__BUILD_DATE__: JSON.stringify('2026-10-16'),
},
});// anywhere in src/
footer.textContent = `v${__APP_VERSION__}`;For TypeScript, declare them: declare const __APP_VERSION__: string;.
Using a different config file
npx vite --config vite.staging.config.js
VITE v8.3.4 ready in 301 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
➜ press h + enter to show help
Vite restarts the dev server automatically when the config file (or a file it imports) changes.
Tip: Most CLI flags mirror config options:
--port,--host,--open,--base,--outDir,--mode. Flags override the config file, which is handy for one-off runs.
Debugging the resolved config
Run with --debug config to see the final config, or add a tiny plugin with a configResolved(config) hook and log what you need. You'll learn about plugin hooks in the Plugins lesson.
Test yourself
What does defineConfig do at runtime?
- Validates and throws on bad options
- Returns its argument unchanged; it exists for type hints
- Starts the dev server
- Merges with defaults
What arguments does a function-style config receive?
(req, res){ command, mode, ... }- The package.json
- Nothing
Why wrap strings in JSON.stringify inside define?
- For performance
- Because define values are inserted as raw code, so strings need quotes
- To encrypt them
- Vite requires JSON files