The Wild

Webpack to Vite Migration: Step-by-Step Guide

· 6 min read · Mammals

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Migrating from webpack to Vite replaces your slow, configuration-heavy build with a faster, simpler one. Vite uses native ES modules for development (instant start, hot reload) and Rollup for production builds. This guide walks you through the process, covering the necessary changes to your project files, dependencies, and environment variables.

What You'll Need

  • Your existing webpack project (source code, package.json, webpack.config.js)
  • Node.js version 18+ or 20+ (check Vite's official requirements)
  • A terminal or command line tool
  • A code editor (optional but helpful)

Step-by-Step

  1. Back up your project. Copy your entire project folder or commit all changes to version control. This ensures you can roll back if something breaks.
  2. Remove webpack dependencies. In your package.json, delete all webpack-related packages: webpack, webpack-cli, webpack-dev-server, html-webpack-plugin, css-loader, style-loader, file-loader, url-loader, babel-loader, ts-loader, vue-loader (if using Vue), and any other loaders or plugins. Also remove the webpack.config.js file (or webpack.config.ts) from your project root.
  3. Install Vite and necessary plugins. Run npm install -D vite (or yarn add -D vite / pnpm add -D vite). Depending on your framework, install the corresponding plugin: @vitejs/plugin-react for React, @vitejs/plugin-vue for Vue, @vitejs/plugin-vue-jsx for Vue JSX, @vitejs/plugin-legacy for older browser support, and vite-plugin-html if you need to customize the HTML template.
  4. Create a vite.config.js file. In your project root, create a file named vite.config.js (or .ts). At minimum, it should look like this:
    import { defineConfig } from 'vite';
    import react from '@vitejs/plugin-react'; // or vue()
    
    export default defineConfig({
      plugins: [react()],
    });
    Add any additional configuration you had in webpack: aliases, proxy for dev server, build output directory, etc. See the Common Mistakes section for how to translate webpack options.
  5. Move your index.html to the project root. Vite uses index.html as the entry point. If yours is in public/ or src/, move it to the root. In that HTML file, change any script or style tags that referenced bundled files (like /bundle.js) to reference your source entry file, e.g., <script type="module" src="/src/main.js"></script>. Remove any webpack-specific HTML comments or placeholders.
  6. Update your package.json scripts. Replace the webpack scripts with Vite ones:
    "scripts": {
      "dev": "vite",
      "build": "vite build",
      "preview": "vite preview"
    }
    If you had a start script, point it to vite as well.
  7. Adjust environment variables. Webpack uses process.env.NODE_ENV and custom variables via DefinePlugin. Vite exposes variables prefixed with VITE_ on import.meta.env. Replace all process.env.MY_VAR with import.meta.env.VITE_MY_VAR. For NODE_ENV, use import.meta.env.MODE or import.meta.env.DEV/PROD. Update your .env files to prefix custom variables with VITE_.
  8. Handle static assets. In webpack, you might have used require('./image.png') or import img from './image.png'. Vite supports both, but you must ensure the file is in the correct location. Assets imported in code are automatically hashed and served. Files in the public/ directory are served as-is at the root path. Move any files you referenced with absolute paths (like /images/logo.png) into public/.
  9. Update CSS imports. Vite supports CSS imports in JavaScript and also handles @import in CSS. If you used CSS modules, they work the same way: import styles from './App.module.css'. For global CSS, just import it in your main JS file.
  10. Test the dev server. Run npm run dev and open the local URL. Check for any errors in the console. Fix any missing imports or syntax issues.
  11. Build for production. Run npm run build. Vite will output to dist/ by default. Check the output for any missing assets or incorrect paths. If you had a custom output directory in webpack, set build.outDir in your Vite config.
  12. Update your CI/CD pipeline. If you have automated builds, change the build command from webpack --mode production to vite build. Also update any references to webpack in your deployment scripts.

Common Mistakes

  • Forgetting to move index.html. Vite requires it at the root, not in public/.
  • Using process.env directly. Vite does not define process.env in the browser. Use import.meta.env instead.
  • Not updating aliases. If you had resolve.alias in webpack, you must copy that to resolve.alias in Vite, but the syntax is slightly different: use path.resolve(__dirname, 'src') instead of path.join.
  • Leaving webpack-specific code. Things like require.context or import.meta.webpackHot will break. Replace them with Vite's import.meta.glob or import.meta.hot.
  • Assuming all loaders have Vite equivalents. Some webpack loaders (e.g., for SVGs as React components) need a Vite plugin like vite-plugin-svgr.
  • Not cleaning up old files. Remove webpack.config.js and any leftover webpack-specific files like .babelrc if you were using babel-loader (Vite uses esbuild for transpilation, so Babel is often unnecessary).

Tips

  • Use vite-plugin-inspect to see how Vite processes your modules during development.
  • Take advantage of Vite's built-in TypeScript support. You don't need ts-loader; Vite transpiles TS with esbuild.
  • For large projects, migrate incrementally. Start with a small feature branch, get it working, then merge.
  • Check the Vite migration guide for specific framework examples (React, Vue, Svelte, etc.) if you get stuck.
  • Use server.proxy in Vite config to replicate your webpack dev server proxy for API calls.

Troubleshooting

  • Build fails with "Could not resolve" errors. Check your import paths. Vite is stricter about extensions; you may need to add .js or .jsx to relative imports. Alternatively, set resolve.extensions in your Vite config to include missing extensions.
  • Dev server is slow or hangs. This can happen if you have a large node_modules or if you're using a plugin that doesn't support Vite. Try removing plugins one by one to isolate the issue.
  • Hot module replacement (HMR) not working. Ensure your component files export correctly (default or named exports). For React, make sure you're using the React plugin. Sometimes a full page reload is needed after config changes.
  • CSS not loading. Check that your CSS is imported in a JavaScript file or in index.html via a <link> tag. If using CSS modules, verify the file extension is .module.css.
  • Environment variables are undefined. Make sure they are prefixed with VITE_ and that you restarted the dev server after changing .env files.
  • Production build works but assets have wrong paths. Set base in your Vite config if your app is served from a sub-path. For example, base: '/my-app/'.

Frequently Asked Questions

Can I keep using Babel with Vite?

Yes, you can add @vitejs/plugin-react which includes Babel for React fast refresh, but for general transpilation Vite uses esbuild, which is much faster. You can also add a custom Babel plugin if needed.

Will my webpack loaders work in Vite?

No, webpack loaders are not compatible with Vite. You need to find Vite plugins or use Vite's built-in features. For example, instead of sass-loader, just install sass and import .scss files directly.

How do I handle code splitting?

Vite handles code splitting automatically for dynamic imports. You can also configure build.rollupOptions.output.manualChunks to manually split vendor code if needed.

Is Vite production-ready for large applications?

Yes, many large production apps use Vite. Its build output is optimized with Rollup, and it supports features like tree-shaking and minification out of the box.

What about webpack's DefinePlugin?

Use Vite's define option in the config to replace global constants. For environment variables, use import.meta.env.

Can I run Vite and webpack side by side during migration?

Yes, you can keep both configs and switch scripts, but it's easier to do a full switch once you've verified the Vite build works. Running both simultaneously may cause port conflicts.