skip to content

In Vite, how does an SSR build made with `vite build --ssr` differ from the client build, and when do you add a dependency to `ssr.noExternal`?

level: seniorimportance: nice to knowfreq 5%

answer

  1. two builds, two output folders
  2. a flag that names the server entry
  3. dependencies left for Node to import
  4. linked packages are the exception
  5. bundle what Node cannot import

basics

~20 s

vite build --ssr builds a server entry that Node loads with import(): dependencies stay external, output is unminified, and assets are left to the client build. Add a dependency to ssr.noExternal when Node cannot import it as published, so Vite must bundle it.

solid answer

~40 s

An SSR app needs two builds: a normal client build, for example `vite build --outDir dist/client`, and a server build, `vite build --outDir dist/server --ssr src/entry-server.ts`, whose output the server loads with `import()`. The SSR build differs in several ways: `build.minify` defaults to `false`; the entry keeps its name (`entry-server.js`, no hash); static assets are not emitted, because the client build emits them; and dependencies are **externalized** by default, so Node imports them from `node_modules` at runtime, with linked packages the exception. `ssr.noExternal` lists dependencies to bundle instead. You need it when a package cannot be imported by Node as published, such as one that imports its own `.css` files or ships source relying on Vite features. `ssr.noExternal: true` bundles everything, useful for a single-file server bundle.

go deeper

for a junior

Recall that SSR needs a client build and a separate vite build --ssr for the server entry, and that the server imports the SSR output.

for a middle

Explain the SSR build's defaults: no minification, unhashed entry, no emitted assets, and dependencies externalized except linked packages.

for a senior

Diagnose a server that builds but crashes on a node_modules import, decide between ssr.noExternal and fixing the package, and keep resolve conditions consistent in dev and production.

for a principal

Weigh externalized against fully bundled server output for your deploy targets: install size, cold start, Web Worker runtimes, and who owns the dependency list.

## Two builds, not one A server-rendered Vite app runs the same components in two places: the browser, and a server process that renders HTML. Each place gets its own build. 1. **Client build:** `vite build --outDir dist/client`, a normal build that emits hashed chunks, CSS, assets and the `index.html` the server uses as its template. 2. **SSR build:** `vite build --outDir dist/server --ssr src/entry-server.ts`. The **`--ssr`** flag marks an SSR build and names its entry. The config equivalent is `build.ssr`, either the entry path as a string or `true` with the entry set elsewhere. In production the server reads `dist/client/index.html` as the template and loads `dist/server/entry-server.js` with a plain `import()`, so it never needs Vite's dev-time module loader. ## How the SSR build differs | Aspect | Client build | SSR build | |---|---|---| | Minification (`build.minify`) | on, with the default minifier | `false` | | Entry file name | `assets/[name]-[hash].js` | `[name].js`, for example `entry-server.js` | | Static assets | emitted | not emitted; the client build emits them (`build.ssrEmitAssets` changes this) | | Dependencies | bundled | externalized, except linked packages | | Runtime | browsers | `ssr.target`: `'node'` (default) or `'webworker'` | ## What "externalized" means By default an SSR build leaves each dependency as an `import` that Node resolves from `node_modules` when the server runs. This keeps the build fast and the server bundle small. It also means every dependency must be importable by Node exactly as published. - **Linked packages** (for example a workspace package) are **not** externalized by default, so they go through Vite's pipeline. Add one to `ssr.external` if you want it treated like an installed package. - **Externalized imports resolve with** `ssr.resolve.externalConditions`, default `['node', 'module-sync']`. If you change them, run Node with the same `--conditions` flag in dev and in production. ## When `ssr.noExternal` is the fix Add a dependency to **`ssr.noExternal`** (a string, a regex, an array of them, or `true`) when Node cannot import it as published, so it must be bundled through Vite's pipeline: - a component package that imports its own `.css` files, which Node's module loader refuses; - a package that ships untranspiled source relying on Vite features, the case the SSR guide names; The symptom is usually a server that builds without complaint and then fails at startup or on first render, on an import inside `node_modules`. ### Precedence rules - A dependency listed by name in `ssr.external` (the `string[]` form) wins over any `ssr.noExternal` match. - With `ssr.noExternal: true` **and** `ssr.external: true`, `ssr.noExternal` wins and no dependency is externalized. - `ssr.noExternal: true` alone treats every dependency as bundled. The SSR guide uses it for a single-file bundle, typically for a Web Worker runtime with `ssr.target: 'webworker'`, and notes that importing a Node.js built-in then throws an error. ## Diagnosing an externalization failure 1. **Read the stack trace.** If the failing import is inside `node_modules` and points at a `.css` file, an unusual extension or untranspiled syntax, Node is loading a package it cannot run as published. 2. **Confirm it is externalized.** Open the SSR output: an externalized dependency appears as a bare `import` of the package name rather than as bundled code. 3. **Add it to `ssr.noExternal`** and rebuild. The package's code now goes through Vite's pipeline and is bundled into the server output. 4. **Compare dev and production.** Externalization applies in dev too, so if the package worked in dev and fails only after the build, check what differs between the two runs, such as resolve conditions, before reaching for `ssr.noExternal`. ## Preload directives for server-rendered pages To render `<link rel="modulepreload">` tags for the chunks a page actually used, run the **client** build with `--ssrManifest`. It writes `dist/client/.vite/ssr-manifest.json`, which maps module ids to their client chunks and assets. It comes from the client build because that is where the browser files are. How the render step collects the module ids it used belongs to the framework's server-rendering API, not to Vite's build.

  • Which build produces `.vite/ssr-manifest.json`, and what is it for?
    The client build, run with `--ssrManifest` (or `build.ssrManifest: true`), writes it into the client output, for example `dist/client/.vite/ssr-manifest.json`. It maps module ids to their client chunks and asset files, so the server can render preload directives, or 103 Early Hints, for the modules a page actually rendered.
  • A workspace UI package renders fine through the SSR build, but a teammate wants to check it the way it would behave once published to npm. What do you change?
    Linked packages are not externalized by default, so Vite bundles them through its pipeline (in dev, that also gives them HMR). Adding the package's name to `ssr.external` makes the SSR build leave it as a Node import like any installed dependency, which shows whether it works as published.

  • An SSR build bundles all of node_modules into the server file by default
  • The SSR build is minified like the client build
  • One vite build --ssr run produces both the client and the server bundles
  • The SSR build generates the SSR manifest for preload directives
  • ssr.noExternal: true with ssr.external: true externalizes everything