· 3 min read

Building React Applications with Modern Tools: From Create React App to Vite

In this blog post, we'll explore how to create a new React application using both Create React App and Vite.

Building React Applications with Modern Tools: From Create React App to Vite

React has been a cornerstone in the modern web development landscape, offering a robust framework for building interactive user interfaces. Traditionally, developers have used Create React App (CRA) to bootstrap new React projects. However, with the advent of faster and more flexible tools like Vite, the ecosystem has expanded to offer improved development experiences.

In this blog post, we'll explore how to create a new React application using both Create React App and Vite. We'll dive into the options each tool provides, helping you choose the best fit for your next project.

1. Introduction to Create React App

Create React App is an officially supported way to create single-page React applications. It offers a modern build setup with no configuration, allowing developers to focus on writing code rather than setting up tooling.

Features

  • Zero Configuration: No need to manually configure Babel, Webpack, or other tools.
  • Development Server: Comes with a development server with hot module replacement.
  • Production Builds: Optimizes the app for production out of the box.
  • Testing: Integrated testing environment using Jest.

2. Creating a React App with CRA

To create a new React application using CRA, follow these steps:

Prerequisites

  • Node.js: Ensure you have Node.js installed (version 14.0.0 or higher is recommended).
  • npm or Yarn: Comes bundled with Node.js.

Steps

  1. Open TerminalOpen your terminal or command prompt.
  2. Run the Create React App CommandUsing npx (which comes with npm 5.2+):npx create-react-app my-appOr using npm:npm init react-app my-appOr using Yarn:yarn create react-app my-app
  3. Navigate to the App Directorycd my-app
  4. Start the Development Servernpm startThis will start the development server at http://localhost:3000.

3. Understanding CRA Options

While CRA aims for zero configuration, it does provide some options:

Template Selection

You can specify a template when creating your app:

  • TypeScript Templatenpx create-react-app my-app --template typescript
  • Custom TemplatesUse community templates by specifying --template [template-name].

Proxy Setup

To set up a proxy for API calls:

  • Edit the package.json file:"proxy": "http://localhost:4000",

Environment Variables

CRA supports environment variables starting with REACT_APP_:

  • Create a .env file:REACT_APP_API_KEY=your-api-key

4. Introduction to Vite

Vite is a modern frontend build tool that significantly improves the development experience. Created by Evan You, the creator of Vue.js, Vite leverages native ES modules in the browser to provide a faster, leaner development environment.

Features

  • Lightning-Fast Cold Starts: Uses native ES modules, eliminating the need for bundling during development.
  • Hot Module Replacement (HMR): Provides instant updates without full page reloads.
  • Rich Plugin Ecosystem: Supports Rollup-compatible plugins.
  • Framework Agnostic: Works with React, Vue, Svelte, and more.

5. Creating a React App with Vite

Prerequisites

  • Node.js: Ensure Node.js is installed.
  • npm or Yarn: Comes with Node.js.

Steps

  1. Run the Create Vite CommandUsing npm 6.x:npm init vite@latest my-vite-app --template reactUsing npm 7+:npm init vite@latest my-vite-app -- --template reactUsing Yarn:yarn create vite my-vite-app --template react
  2. Navigate to the App Directorycd my-vite-app
  3. Install Dependenciesnpm install
  4. Start the Development Servernpm run devThe app will be running at http://localhost:3000 (or another available port).

6. Understanding Vite Options

Vite offers various options to customize your development setup.

Templates

Vite supports multiple templates:

  • React with JavaScriptnpm init vite@latest my-app -- --template react
  • React with TypeScriptnpm init vite@latest my-app -- --template react-ts
  • Vue, Svelte, Preact, Lit, etc.Replace react with the desired framework.

Configuration File

Vite uses vite.config.js for configuration.

  • Alias Configuration
    import { defineConfig } from 'vite';
    import react from '@vitejs/plugin-react';
     
    export default defineConfig({
      plugins: [react()],
      resolve: {
        alias: {
          '@components': '/src/components',
        },
      },
    });

Environment Variables

  • Mode-Based VariablesCreate .env files like .env.development and .env.production.
  • Accessing VariablesVariables should start with VITE_:VITE_API_URL=https://api.example.comAccess in code:const apiUrl = import.meta.env.VITE_API_URL;

Build Options

Customize the build process in vite.config.js:

export default defineConfig({
  build: {
    outDir: 'dist',
    sourcemap: true,
  },
});

7. Comparing CRA and Vite

Performance

  • CRA: Slower startup and rebuild times due to Webpack bundling.
  • Vite: Faster cold starts and HMR due to native ES modules.

Configuration

  • CRA: Zero configuration but less flexible. Requires ejecting for customization.
  • Vite: Highly configurable without ejecting.

Ecosystem Support

  • CRA: Officially maintained by the React team.
  • Vite: Rapidly growing community with support for multiple frameworks.

8. Conclusion

While Create React App remains a solid choice for beginners and those who prefer convention over configuration, Vite offers a modern alternative with superior performance and flexibility. By understanding the options and capabilities of both tools, you can select the one that best suits your project's needs.

Happy Coding with React and Modern Tooling!

Written by

Felix Yu

Full stack developer in Melbourne and founder of Pixdyne. Also Shuhai Yu on GitHub and LinkedIn.

About meCase studiesGitHub

Got something to make?
Write to me.

info@felixyu.net