Vite Library Mode: Build a Package That Leaves Peer Dependencies to the Consumer
Use Vite library mode to publish a component package while keeping peer dependencies such as React out of your bundle.
10 Apr 2026, 20:35 UTC

The problem and the useful takeaway
You are maintaining a UI component library. Your consumers use their own bundlers—webpack, Vite, or Rollup—and they already install React or Vue. If your published package bundles React inside its JavaScript, consumers end up with two copies: one in your library, one in their app. That breaks hooks, context, and component identity.
The useful takeaway: use Vite library mode (build.lib) to emit ES and UMD bundles, mark your peer dependencies as external, and let the consumer resolve them. Vite handles the build, but you must wire up package.json exports yourself and verify that nothing you marked external accidentally ends up inside the bundle.
Requirements
This design assumes Vite 5. Vite 6 or later may change configuration defaults, so verify your installed version with npx vite --version before copying the configuration below.
You need:
- A single JavaScript or TypeScript entry file that exports your public API.
- Consumers who run a bundler or a modern browser that supports ES modules.
- Peer dependencies declared in
package.json, such asreact,react-dom, orvue.
Terms used below:
- Peer dependency: a library your package expects the consumer to install separately.
- External: a build instruction that leaves an import statement in the output instead of copying the dependency into the bundle.
- Tree-shaking: removing unused code from the final bundle.
Smallest suitable design
Configure one entry point, a library name, and the output formats. Vite delegates bundling to Rollup while handling Vite plugins and CSS extraction.
// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
build: {
lib: {
entry: src/index.ts,
name: 'AcmeUI',
formats: ['es', 'umd'],
},
rollupOptions: {
external: ['react', 'react-dom'],
output: {
globals: {
react: 'React',
'react-dom': 'ReactDOM',
},
},
},
},
});
Run from the library project root:
npx vite build
No special permissions are required. Vite writes to build.outDir, which defaults to dist. Expected result: a dist folder containing ES and UMD output, plus any extracted CSS assets. Do not assume exact filenames; they vary by Vite version.
Then update package.json manually so consumers can find the generated files:
{
"main": "dist/umd/acme-ui.umd.js",
"module": "dist/es/acme-ui.es.js",
"exports": {
".": {
"import": "./dist/es/acme-ui.es.js",
"require": "./dist/umd/acme-ui.umd.js"
},
"./dist/style.css": "./dist/style.css"
},
"peerDependencies": {
"react": "^18.0.0",
"react-dom": "^18.0.0"
}
}
Trust and data boundaries
Marking react as external shifts resolution to the consumer. Your bundle keeps an import statement for React instead of inlining it. If the consumer installs your package but forgets React, that import resolves to nothing at runtime. The trust boundary is the import statement left in your output: your library trusts that the consumer's module graph provides a compatible binding.
CSS is another boundary. Vite can extract styles into a separate .css file. If a consumer imports only your JavaScript entry, styles may not load because CSS is a side effect, not a JavaScript dependency. Document that consumers must import the CSS entry explicitly.
Operational checks
- Build. Run
npx vite buildin the library project root. - Inspect output. On macOS or Linux, run
find dist -type f -maxdepth 3. On Windows PowerShell, runGet-ChildItem dist -Recurse. Confirm peer libraries such as React do not appear inside your bundle files. - Consumer smoke test. Create a temporary consumer project, install your package with
npm install file:../path-to-library, import a component, and start the consumer's dev server. Check the browser console for "Cannot find module" or React invalid hook errors, which indicate a missing peer dependency. - Size comparison. Run
npx vite buildwithexternal: ['react'], then again withexternal: [], and comparedistsizes. The external build should be significantly smaller.
Risk: npm install file:... mutates the consumer's node_modules. Remove the test installation after verification.
Failure modes
- Missing peer dependency. The consumer lacks React; your UMD bundle references a
Reactglobal that does not exist. - Global conflict. UMD exposes
AcmeUIonwindow; if another library uses the same global, behavior is undefined. - CSS side effect ignored. The consumer imports JavaScript only; components render unstyled.
- HMR mismatch. The Vite dev server remains usable during library development, but hot module replacement is optimized for application entry points and may behave differently for library-only builds.
- Tree-shaking variance. Source maps and minification are supported through
build.rollupOptionsandbuild.minify, but final tree-shaking quality for ES modules depends on the consumer's bundler.
Conditions that change the design
Do not use library mode if:
- You are building a multi-page application or a browser-direct single-page application. Library mode does not generate HTML entry points or optimize for application routing.
- Your package is meant to be consumed directly in a browser via
<script type="module">without a consumer bundler. ES module output assumes a consumer bundler or import maps. - You need multiple independent entry points in one package.
build.libexpects one entry; use multiple Rollup configurations or keep the project in Vite's default application mode.
Rollback
vite build writes to dist. To roll back, delete dist and revert vite.config.ts and package.json to their previous state. No database or migration state is involved.