To get started with React, you need to set up a proper development environment. While you can technically add React to a simple HTML file using <script> tags, the standard and most efficient way to build React applications is by using a modern build toolchain.
This toolchain sets up your development environment so you can use the latest JavaScript features, provides a fantastic developer experience (like hot-reloading), and optimizes your application for production deployment.
Before you can create your first React application, you must have a few essential tools installed on your computer:
npm (Node Package Manager). You can download and install Node.js from their official website.To verify that Node.js and npm are installed, open your terminal and run:
node -v npm -v
If both commands return version numbers, you are ready to go!
Historically, the official way to create a React application was using Create React App (CRA). However, in recent years, Vite has emerged as the modern, blazing-fast alternative that most developers and the official React documentation now recommend.
We will cover both methods below.
Vite is a modern build tool that provides a significantly faster and leaner development experience compared to CRA. It starts up almost instantly and updates the browser incredibly fast when you save your files.
To create a new React app using Vite, open your terminal, navigate to the folder where you want your project to live, and run:
npm create vite@latest my-first-react-app -- --template react
Once the process completes, you need to navigate into your new project folder and install the necessary dependencies:
cd my-first-react-app npm install
If you are following an older tutorial or joining an existing codebase, you might still see create-react-app. You can create an application by running the following command using npx (a package runner tool that comes with npm):
npx create-react-app my-first-react-app
Once the installation is complete, navigate into your new project folder and start the development server:
cd my-first-react-app npm start
This will automatically open your new React application in your default web browser. You are now ready to start building your React application!