Skip to main content
← Guides

Guide

Migrate from Webpack to Vite: A Step-by-Step Guide 2026

Updated PkgPulse Team6 min read
0

Migrate from Webpack to Vite step by step: config mapping, env variables, plugin equivalents, legacy CommonJS dependencies and common gotchas to check first.

Contents

TL;DR

Move a React app from Webpack to Vite by running the two builds side by side until the Vite build passes the same deployment checks. Move index.html to the Vite root, expose only public settings through import.meta.env, and test the imports and loader behavior your app relies on. Switch the deployment command after you can open a direct URL, navigate to a lazy route and load the same API and assets from the Vite build.

Key Takeaways

  • Vite 8 uses Rolldown for production builds and dependency optimization. The v6 React plugin uses Oxc by default and drops the Babel options on react().
  • Keep the Webpack config and lockfile baseline until the Vite output passes staging. Review custom loaders, federation and browser support before changing your pipeline.
  • Validate TypeScript separately from bundling. Move client settings from REACT_APP_* to VITE_* and keep credentials on the server.

Step 1. Install Vite alongside Webpack

Capture a passing Webpack build first. Record the Node runtime, public path, output directory, deployment command and any files your host expects. Pick a route that lazy-loads a component and makes an API request. Save the expected behavior for use in both builds.

The React example uses Vite 8.4.1 with @vitejs/plugin-react 6.3.1. Both packages declare Node ^20.19.0 || >=22.12.0. Check the Node runtime in CI as well as the one on your laptop. For Vue, use the Vue plugin with a peer range that covers your Vite release. If your Webpack build depends on a custom Babel transform, try @rolldown/plugin-babel and verify its output on a file that relies on the transform.

npm install -D --save-exact vite@8.4.1 @vitejs/plugin-react@6.3.1

Step 2. Create vite.config.ts

Use the existing deployment directory if your host requires it. Set base to your production public path if the app is served under a subdirectory. If you use TypeScript path aliases, Vite can read them with resolve.tsconfigPaths. Check any corresponding test-runner aliases separately.

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  resolve: { tsconfigPaths: true },
  server: {
    proxy: {
      '/api': { target: 'http://localhost:8080', changeOrigin: true },
    },
  },
  build: { outDir: 'build-vite', manifest: true },
});

Use a separate output directory while you compare builds. The example proxy points to an existing local API and applies to Vite development. Wire the production API path on your host or reverse proxy, and repeat the same request against staging. If your backend writes HTML, update its asset lookup to read Vite's manifest and verify the emitted script and CSS URLs.

Step 3. Add parallel scripts

Install your project's TypeScript version if it isn't already a dependency. Keep the existing test command while you change bundlers. A later Jest-to-Vitest migration can have its own baseline for mocks and timers.

{
  "scripts": {
    "dev:vite": "vite",
    "build:vite": "tsc --noEmit && vite build",
    "preview:vite": "vite preview"
  }
}

Vite transforms TypeScript and JSX for the app. The tsc step catches type errors. If your project uses TypeScript project references, carry over your existing type-check command.

Step 4. Add index.html at the project root

For Create React App, start with a copy of public/index.html so the Webpack baseline still has its template. Remove CRA-only placeholders such as %PUBLIC_URL% from the Vite copy. Add an explicit module entry and keep the same mount element your React entry expects. Assets in public/ can be referenced by their public path. Test the resulting URLs with your production base.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>My App</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/index.tsx"></script>
  </body>
</html>

For multiple HTML entries, map them with build.rolldownOptions.input and request each built path. Keep the old entry until all routes and redirects work on the new host configuration.

Step 5. Migrate public environment variables

Audit the call sites before renaming variables. Change browser references such as process.env.REACT_APP_API_URL to import.meta.env.VITE_API_URL, and add the new public key to the corresponding environment files or CI settings. Vite substitutes client values during the build, so changing a variable on the host after upload does not change the emitted JavaScript.

const apiUrl = import.meta.env.VITE_API_URL;
const enableFeature = import.meta.env.VITE_FEATURE_FLAG === 'true';
const isDev = import.meta.env.DEV;

Custom values arrive as strings. Use DEV or PROD when you need a boolean and MODE when you need the chosen mode name. Include vite/client in your TypeScript declarations and declare any custom variables in ImportMetaEnv. Check values in a built staging app. A variable with the VITE_ prefix is public, so keep credentials in the server's configuration.

Step 6. Map Webpack loaders and plugins

Existing Webpack behaviorVite migrationCheck
HtmlWebpackPlugin injects the entryStart from root index.html and an explicit module scriptThe app mounts on a direct request
MiniCssExtractPlugin emits CSSStart with Vite's built-in CSS handlingStyles load on the initial page and on lazy routes
DefinePlugin replaces compile-time constantsUse Vite's define for explicit public constantsInspect the built value in both modes
Static files copied by a pluginUse public/ for assets that need fixed namesFile names and paths match what the host expects
SVG loader returns a React componentUse a compatible SVG transform when the caller needs a componentOpen the component and verify its props and styles
A custom Webpack loader transforms a proprietary filePort the transform to a compatible Vite pluginExercise a representative import in dev and in the build

A plain SVG asset import and an SVG-to-component transform have different contracts. Check the call sites before choosing a replacement. For custom plugins, check whether their hooks run on dev requests or only on builds. A clean dev page gives little evidence about build-only asset generation.

Common Gotchas

CommonJS dependencies. Vite pre-bundles dependencies for the dev server. For a linked CommonJS package that the optimizer skips, try adding that specific package to optimizeDeps.include. Run the production build separately. build.commonjsOptions has no effect in Vite 8, so inspect the package's ESM entry or CommonJS export shape if a built import fails. If the package expects Node-only APIs in the browser, prefer a browser entry or replace that dependency.

Webpack source APIs. Replace a browser-side require() with an ESM import where possible. For a directory of dynamically loaded modules, review import.meta.glob and check whether the old call sites expected eager or lazy loading.

CSS Modules. Keep the .module.css imports and check visual behavior before updating snapshots that include generated class names. For a project with custom CSS loaders, verify any file-name convention and the order of global styles.

Verify the Migration

Run the dev server and open the same initial route you recorded in Webpack. Edit a stateful React component and check the expected refresh behavior. Build with npm run build:vite, open the built output with the preview command and request a lazy route both directly and from inside the app. Deploy the candidate to staging to check host rewrites, the real API proxy, asset paths, caching and any browser targets you support. Once those checks pass, switch CI and remove the Webpack dependencies you no longer use.

Methodology

The React build fixture covers the config, the module entry, TypeScript and JSX transforms, a public env variable and a lazy import. It checks the manifest and files on disk. The production proxy, browser refresh and custom plugins depend on your app and are checks for your own staging release.

Compare Vite and Webpack package health on PkgPulse.

See also: Bun vs Vite and Turbopack vs Vite, Migrating Webpack to Vite in 2026: Step-by-Step for Large Apps.

Source notes