Skip to content
← ALL POSTS

Migrating your Create React App project to Vite

In one of my previous articles, I have discussed why Vite is faster and how you can start a new project with Vite.

ABUL HASNAT6 MIN READ

In one of my previous articles, I have discussed why Vite is faster and how you can start a new project with Vite.

Today I will be focusing on how you can migrate your existing react project created with create-react-app.

If you already have an existing react project, then it is a bit complicated process to move to Vite. Don’t get confused with the steps necessary. I will try my best to describe the steps one by one.

First of all, delete the node_modules folder of your project because we will be configuring the package.json file for Vite.

Mandatory Steps

The first step is to remove all the react-script dependencies from the  package.json file and add vite. Note that, the version of the packages may change with time. It is recommended to always use the latest version.

A package.json diff removing react-scripts from dependencies and adding a devDependencies block with @vitejs/plugin-react and vite

Run npm install or yarn. Replace scripts in package.json.

A package.json diff replacing the four react-scripts commands, start, build, test and eject, with two: start running vite and build running vite build

Move public/index.html to index.html (project root folder). This is necessary. Otherwise, Vite will not work.

Now we need to edit the index.html file and make some modifications there. First of all, remove all the  %PUBLIC_URL% from index.html:

An index.html diff rewriting three tags, the icon, apple-touch-icon and manifest links, so each href drops the %PUBLIC_URL% prefix and starts from the site root instead

Add entry point in index.html:

An index.html diff adding a module script tag pointing at /src/index.jsx, immediately after the root div

If you are using typescript, then use your typescript entry point (i.e. /src/index.tsx).

Next step to create a vite.config.js or vite.config.ts file at the root of your project. The file should look like this:

A minimal vite.config.js: it imports defineConfig from vite and the React plugin, and exports a config whose only setting is that one plugin

Go to your src folder, create a new file vite-env.d.ts and add this line to the file: /// <referencetypes=”vite/client” /> (You have to add ///)

If your project is small enough, there is a possibility that these are the only steps that you need to follow. But there are more things to be configured for big projects.

Handling environment variables

Let’s assume this is your .env file:

A Create React App .env file setting PORT, PUBLIC_URL, REACT_APP_NAME and REACT_APP_VERSION

In vite, by default, all the environment variables start with VITE_. So all the environment variables that start with REACT_APP_ are needed to be replaced with VITE_.

A .env diff renaming REACT_APP_NAME to VITE_NAME, the prefix Vite expects, with the value unchanged

This means you might need to change all your environment variables which may be a bit painful. This is where another plugin comes into play called vite-plugin-env-compatible. Run the command npm i vite-plugin-env-compatible or yarn add vite-plugin-env-compatible. Then add the following to your vite.config.js or vite.config.ts file.

A vite config importing vite-plugin-env-compatible, setting envPrefix to REACT_APP_ and adding envCompatible to the plugin list, so the existing variable names keep working

The envPrefix tells vite what should be the staring of every env variable. Thus you don’t need to change any of the env variable name. But you have to do one thing for sure. Wherever you used process.env, you have to change it to import.meta.env. This can be easily done by searching in the vscode and replacing it.

A diff in a React component changing process.env.REACT_APP_NAME to import.meta.env.REACT_APP_NAME

Additional Configuration

If you are using typescript, then you definitely have one tsconfig file where you might have configured your path aliases. In vite, in order for tsconfig paths to work, you need to install vite-tsconfig-paths using npm or yarn. Then you have to add this plugin to your vite config file.

The vite config with vite-tsconfig-paths imported and tsconfigPaths added to the plugin list, so the path aliases in tsconfig resolve

There is a possibility that you may use aws-amplify package in your project in order to use cognito authentication. This may not work out of the box. You have to edit the vite config file and add one extra line for aws-amplify to work.

The vite config gaining a resolve.alias entry that maps ./runtimeConfig to ./runtimeConfig.browser, which is what aws-amplify needs in the browser

This './runtimeConfig': './runtimeConfig.browser' will make sure that aws-amplify is running smoothly in your project.

Sometimes you may use one or more package that uses Node.js built-in and globals. In vite, build in and globals will not work out of the box. You have to do some configuration in vite config file as well as in the index.html file. Let’s first configure it in vite config file. First you have to install @esbuild-plugins/node-globals-polyfill plugin using npm or yarn. Then add this to the vite config file.

The vite config gaining an optimizeDeps.esbuildOptions block that defines global as globalThis and enables NodeGlobalsPolyfillPlugin with buffer turned on

Then add the following lines into your index.html file inside the script tag.

A script tag for index.html assigning window.global to window and declaring an empty exports object

These two steps will make sure that global works anywhere in your project.

Lastly, there is possibility that you are using svg files in your project. And in react you can import svg files as react component by using import {ReactComponent as logo} from 'your-svg-path'. This will throw an error in vite. To fix this you have to install vite-plugin-svgr using npm or yarn then add it to your vite config file.

The finished vite config, with all six plugins imported and svgrPlugin added last, configured with svgrOptions icon set to true so SVGs can be imported as components

Then you have to go to your src folder, and edit vite-env.d.ts file and add another line: /// <referencetypes=”vite-plugin-svgr/client” /> (You have to add ///). After that import {ReactComponent as logo} from 'your-svg-path' this import will work perfectly fine.

This is how you can move to Vite in your existing project. The process might take a bit long time and some digging, but once you have moved to vite, you will be saving a lot of times every time you run your dev server and build files for your production server. Here is a comparison of mine how fast I got using Vite:

Dev server in create react app Dev server in Vite
53 seconds 7 seconds

You can easily see how much faster I got when I switched to vite. I hope this article might help you to migrate to Vite as well.

NEWSLETTER

One engineering letter a month

New writing from our engineers, no marketing filler.

[ ONE CONVERSATION AWAY ]

Rather ask an engineer than read another post?

Thirty minutes, no sales script. Bring the problem you are actually stuck on and we will tell you honestly whether we are the right partner for it.