Course contentsShow
← Web Development
Lesson 1385 of 2,98225. Build Tools and BundlersFree lesson

Library Mode in Vite

Building reusable JavaScript libraries with Vite using library mode configuration.

Library Mode in Vite

You'll learn how to configure Vite to build reusable JavaScript libraries that can be published and consumed by other projects.

What Is Library Mode?

Until now, you've used Vite to build applications—complete websites with HTML entry points. But what if you're building a reusable component library, utility package, or plugin that other developers will install via npm? That's where library mode comes in.

Think of it like the difference between building a complete house versus manufacturing bricks that others will use to build their own houses. Library mode optimizes your build output for distribution as a package, not as a standalone website.

How Library Mode Works

In your vite.config.js, you switch Vite into library mode by configuring the build.lib option:

import { defineConfig } from 'vite';

export default defineConfig({
  build: {
    lib: {
      entry: './src/index.js', // Your library's entry point
      name: 'MyLibrary', // Global variable name (for UMD builds)
      fileName: (format) => `my-library.${format}.js`
    }
  }
});

This tells Vite: "Don't build an HTML app—build a library from this entry file." Vite will automatically generate multiple output formats (ESM, UMD, etc.) so your library works in different environments.

What Gets Generated

When you run vite build, library mode produces:

  • ESM bundles for modern JavaScript projects
  • UMD bundles for browser <script> tags
  • No HTML file—just JavaScript ready to be imported

Your library can export components, functions, or utilities that others install with npm install your-package.

Key Takeaway: Library mode transforms Vite from an application bundler into a package distribution tool, optimizing your code for consumption by other developers rather than end-users.