Run this yourself. The full three-environment vite.config.ts from this guide:
vite-6-environment-api/in the Coding Dunia code-examples repo.
The Vite Environment API is a Vite 6 feature that lets a single config file define multiple build targets, client, server-side rendering, and edge, as named environments, each with its own module resolution, plugins, and build output. A common pre-6 setup was three Vite config files, one for the client bundle, one for SSR, one for a Cloudflare Workers edge deploy, each duplicating most of the same plugin list with small variations. Every plugin update meant editing three files and hoping nothing got missed. The Environment API collapses that into a single config with three named environments.
Multiple runtimes in one app
A typical SSR app runs in at least two different JavaScript environments: the browser, where React hydrates and DOM APIs exist, and the server (Node.js, or an edge runtime like Cloudflare Workers), where there's no window and module resolution rules can differ. Before Vite 6, handling both meant either:
- Running two separate Vite dev servers and gluing their output together, or
- One config file full of
if (isSSR) { ... } else { ... }branches for plugins, aliases, and build targets.
Neither scales cleanly once you add a third target, an edge runtime with its own module resolution quirks (no Node built-ins, different exports conditions in package.json). Every extra target also costs you shipped bytes, which is why it pays to watch bundle size budgets in CI once more than one build comes out of the same config.
Most of these exports condition mismatches trace back to how your compiler is configured, not to Vite itself. Rather than hand-tune moduleResolution and lib by trial and error, our tsconfig generator builds a known-good config from a few toggles, so you can start from a baseline that already matches your target runtime and adjust from there.
Defining named environments
// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
environments: {
client: {
build: {
outDir: 'dist/client',
rollupOptions: { input: './src/entry-client.tsx' },
},
},
ssr: {
build: {
outDir: 'dist/server',
rollupOptions: { input: './src/entry-server.tsx' },
ssr: true,
},
resolve: {
conditions: ['node'],
},
},
edge: {
build: {
outDir: 'dist/edge',
rollupOptions: { input: './src/entry-edge.tsx' },
},
resolve: {
conditions: ['worker', 'browser'],
noExternal: true, // bundle everything, edge runtimes often can't resolve node_modules at runtime
},
},
},
plugins: [react()],
});
Each environment gets its own resolve.conditions, which determines which package.json exports field wins when a dependency ships different code for Node versus the browser versus edge runtimes. That's the detail that used to cause the most subtle bugs, a package resolving to its Node build inside an edge bundle, then crashing at runtime on a missing fs import.
The config above defines three named environments in roughly 30 lines, replacing what used to take three separate config files of comparable length each. The client environment needs no explicit resolve.conditions override because Vite's default already targets the browser. The ssr environment sets conditions: ['node'] so dependencies resolve their Node build. The edge environment sets conditions: ['worker', 'browser'] plus noExternal: true, because most edge runtimes, Cloudflare Workers included, cannot resolve packages from node_modules at request time and need everything bundled into the output file ahead of deploy.
I set up exactly this config with three trivial entry files and ran vite build --app (Vite 6.4.3, Node 22.23.2) to confirm it actually builds all three targets from the one config, not just that the types check:
$ npx vite build --app
vite v6.4.3 building for production...
✓ 1 modules transformed.
dist/client/assets/entry-client-l0sNRNKZ.js 0.00 kB │ gzip: 0.02 kB
✓ built in 16ms
vite v6.4.3 building SSR bundle for production...
✓ 1 modules transformed.
dist/server/entry-server.mjs 0.08 kB
✓ built in 5ms
vite v6.4.3 building SSR bundle for production...
✓ 1 modules transformed.
dist/edge/entry-edge.mjs 0.07 kB
✓ built in 3ms
One command, three separate dist/{client,server,edge} output directories, each respecting its own resolve.conditions and outDir. That --app flag is what tells Vite to build every named environment in the config instead of just the default client target, easy to miss in the docs since it's not needed for a plain single-target build.
A single Vite Environment API config replaces three separate build files that used to duplicate the same plugin list with small, easy-to-miss variations between them.
Why it matters when you never write the config
If you use Astro, the framework already sits on this API. The Astro 6 release post says astro dev is powered by the Environment API and can run your production runtime during development. The practical result is that a problem which used to appear only after deploying to a non-Node target, Cloudflare Workers for instance, can show up on your machine first.
Running one environment programmatically
For tooling authors (framework builders, test runners), the Environment API exposes a programmatic way to run a specific environment's module graph without spinning up a full dev server per target:
import { createServer } from 'vite';
const server = await createServer({
configFile: './vite.config.ts',
});
// Run a module through the 'ssr' environment specifically
const ssrEnv = server.environments.ssr;
const mod = await ssrEnv.transformRequest('/src/entry-server.tsx');
Most app developers never call this directly, it's the layer test runners like Vitest use to execute your code the same way Vite would at runtime, including SSR-specific transforms, instead of running your source through a generic Node require.
According to the Vite Environment API guide, server.environments exposes one entry per named environment defined in the config, each with its own transformRequest method that runs a module through that environment's specific plugin pipeline and module resolution. That matters for tooling authors because it means a test runner can execute a file exactly as the SSR build would, including any SSR-only transforms a plugin applies, instead of falling back to a generic Node require call that skips those transforms entirely. A test runner that uses it doesn't have to reimplement Vite's transform pipeline, so the test and the SSR build are less likely to diverge on edge-case module resolution.
Vite 5 and earlier, side by side with Vite 6
A resolve condition is an entry in Vite's resolve.conditions array that determines which package.json exports field a dependency resolves to for a given environment, for example node, worker, or browser. Getting this wrong per target is a classic source of "works locally, breaks in production" bugs in multi-runtime apps.
| Task | Vite 5 and earlier | Vite 6 Environment API |
|---|---|---|
| Client + SSR build | Two config files or heavy conditionals | Two named environments in one config |
| Edge runtime module resolution | Manual resolve.conditions overrides per build script | Per-environment resolve.conditions |
Dependency exports mismatches | Discovered at runtime, often in production | Still discovered at runtime, but each target's conditions are set in one visible place |
| Plugin list maintenance | Duplicated across config files | Shared plugin list, per-environment overrides only where needed |
To check whether your project would benefit from migrating to named environments, walk through this quick sequence:
- Count how many separate Vite config files or
if (isSSR)branches your build currently has. - Check whether any dependency resolves differently between your client and server bundles, a common sign of an
exportscondition mismatch. - If both are true, define one
environmentsblock with per-targetresolve.conditionsinstead of maintaining separate configs.
When to reach for it directly
Most teams shipping a single-target SPA or a standard SSR app through a mainstream meta-framework never need to write environment configs by hand, the framework owns that layer. Reach for it directly when you're building a custom deploy target the framework doesn't support yet, or when you're authoring a Vite plugin or framework yourself and need consistent module resolution across more than one runtime. The same reasoning applies one layer up, in the TypeScript 7 migration: once the compiler resolves your modules the way the runtime does, the number of places a config can disagree with itself drops.
Have you debugged a dependency that resolved differently in your SSR bundle than your client bundle, and spent an hour before realizing it was an exports condition mismatch? That's exactly the class of bug per-environment resolve.conditions is built to prevent. It doesn't detect a wrong condition for you, though: pick the wrong one and the crash still shows up at runtime, not at build time.
Is the Environment API Stable Yet, Two Majors Later?
No, and this is the part that changed since I first wrote the guide, so it's worth being blunt about it. The build above was run on Vite 6.4.3. Vite is on 8.4.0 as of late November 2026, two majors later, and the Environment API guide in Vite's own documentation still carries this line: the team does not recommend switching to it yet, and asks people to wait until frameworks and plugin authors have validated the design. The API is described as being in a release-candidate phase, with the team committing to stability between major releases while explicitly reserving the right to break specific parts in a future major.
So the config in this guide is not something to migrate a production build onto this week just because it reads more cleanly than three files. Two majors of "don't switch yet" is a signal, not a formality.
That doesn't make the guide moot, for a reason I think is underrated: you are almost certainly already running this API. Astro, Vitest and the other tools named above call it internally, which means when a multi-target build resolves a dependency the wrong way, the stack trace lands in code shaped exactly like the config above. Reading it is how you debug your framework. Writing it is a decision to make later.
| If you are | Then | Because |
|---|---|---|
| Shipping an SPA or a standard SSR app | Ignore this API entirely | Your framework owns the layer, and nothing you write here helps |
| Debugging a resolution bug in Astro, Vitest or similar | Read this guide, change nothing | The internals you are staring at are these environments |
| Authoring a plugin or framework | Build on it now | You are the audience Vite is asking to validate the design |
| Tempted to migrate a production build config | Wait | Vite's own docs say so, and have said so for two majors |
Related Guides
- What's new in create-vite 9.1.2 - the scaffolder side of the same release line, and which templates changed
- React Server Components with TypeScript
- Bun vs Node vs Deno
- Node.js native TypeScript, no ts-node