Webpack to Vite Migration: Step-by-Step Guide
In this article
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
- 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.
- 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 thewebpack.config.jsfile (orwebpack.config.ts) from your project root. - Install Vite and necessary plugins. Run
npm install -D vite(oryarn add -D vite/pnpm add -D vite). Depending on your framework, install the corresponding plugin:@vitejs/plugin-reactfor React,@vitejs/plugin-vuefor Vue,@vitejs/plugin-vue-jsxfor Vue JSX,@vitejs/plugin-legacyfor older browser support, andvite-plugin-htmlif you need to customize the HTML template. - Create a
vite.config.jsfile. In your project root, create a file namedvite.config.js(or.ts). At minimum, it should look like this:
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.import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; // or vue() export default defineConfig({ plugins: [react()], }); - Move your
index.htmlto the project root. Vite usesindex.htmlas the entry point. If yours is inpublic/orsrc/, 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. - Update your
package.jsonscripts. Replace the webpack scripts with Vite ones:
If you had a"scripts": { "dev": "vite", "build": "vite build", "preview": "vite preview" }startscript, point it toviteas well. - Adjust environment variables. Webpack uses
process.env.NODE_ENVand custom variables viaDefinePlugin. Vite exposes variables prefixed withVITE_onimport.meta.env. Replace allprocess.env.MY_VARwithimport.meta.env.VITE_MY_VAR. ForNODE_ENV, useimport.meta.env.MODEorimport.meta.env.DEV/PROD. Update your.envfiles to prefix custom variables withVITE_. - Handle static assets. In webpack, you might have used
require('./image.png')orimport 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 thepublic/directory are served as-is at the root path. Move any files you referenced with absolute paths (like/images/logo.png) intopublic/. - Update CSS imports. Vite supports CSS imports in JavaScript and also handles
@importin 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. - Test the dev server. Run
npm run devand open the local URL. Check for any errors in the console. Fix any missing imports or syntax issues. - Build for production. Run
npm run build. Vite will output todist/by default. Check the output for any missing assets or incorrect paths. If you had a custom output directory in webpack, setbuild.outDirin your Vite config. - Update your CI/CD pipeline. If you have automated builds, change the build command from
webpack --mode productiontovite build. Also update any references towebpackin your deployment scripts.
Common Mistakes
- Forgetting to move
index.html. Vite requires it at the root, not inpublic/. - Using
process.envdirectly. Vite does not defineprocess.envin the browser. Useimport.meta.envinstead. - Not updating aliases. If you had
resolve.aliasin webpack, you must copy that toresolve.aliasin Vite, but the syntax is slightly different: usepath.resolve(__dirname, 'src')instead ofpath.join. - Leaving webpack-specific code. Things like
require.contextorimport.meta.webpackHotwill break. Replace them with Vite'simport.meta.globorimport.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.jsand any leftover webpack-specific files like.babelrcif you were using babel-loader (Vite uses esbuild for transpilation, so Babel is often unnecessary).
Tips
- Use
vite-plugin-inspectto 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.proxyin 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
.jsor.jsxto relative imports. Alternatively, setresolve.extensionsin your Vite config to include missing extensions. - Dev server is slow or hangs. This can happen if you have a large
node_modulesor 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.htmlvia 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.envfiles. - Production build works but assets have wrong paths. Set
basein 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.