Programming & Development

How to Install React and Create Your First Project

If you are learning React, one of the first practical steps is creating your own React project and running it in the browser. Reading about React concepts such…

D
DevByteTechnical Author
Published
13 min read5 views
How to Install React and Create Your First Project

React is one of the most popular JavaScript libraries for building modern, interactive user interfaces. It allows developers to create reusable UI components, manage application state, and build fast web applications.

If you are new to React, getting started can seem confusing. You might wonder which tools you need to install, how to create a project, and how to run your first React application.

In this beginner-friendly guide, you will learn how to install React, create your first project using Vite, understand the project structure, and build a simple React application from scratch.

By the end of this tutorial, you will have a working React application running on your local machine.

What Is React?

React is an open-source JavaScript library used to build user interfaces. It was originally developed at Facebook and is now maintained by Meta and the React community.

React allows developers to divide an application into small, reusable components. Instead of writing an entire interface in one large file, you can create separate components for elements such as navigation bars, buttons, forms, cards, and dashboards.

For example, a website might contain these React components:

  • Navbar — displays the navigation menu.

  • Header — displays the page heading.

  • ProductCard — displays product information.

  • Footer — displays footer content.

These components can be reused across different parts of an application, making the code easier to maintain.

Why Should You Learn React?

React offers several advantages:

  • Reusable components: Build UI elements once and reuse them throughout your application.

  • Declarative UI: Describe how the interface should look based on the current application state.

  • Efficient updates: React updates the necessary parts of the interface when data changes.

  • Large ecosystem: Use libraries for routing, forms, state management, testing, and more.

  • Strong community: Find tutorials, documentation, tools, and community support.

  • Flexible architecture: Build anything from small interactive widgets to complex web applications.

React is useful for beginners learning frontend development and experienced developers building production applications.

Prerequisites Before Installing React

Before creating your first React project, make sure you have the following tools.

1. Basic JavaScript Knowledge

You should understand basic JavaScript concepts, including:

  • Variables and constants

  • Functions and arrow functions

  • Arrays and objects

  • Array methods such as map()

  • ES modules using import and export

  • Basic HTML and CSS

You do not need to be a JavaScript expert to start learning React. However, understanding these concepts will make the learning process easier.

2. Install Node.js

Node.js allows JavaScript to run outside a web browser. React development tools use Node.js to run development servers, install packages, and build applications.

Visit the official Node.js website:

https://nodejs.org/

Download a suitable Long-Term Support (LTS) version and follow the installation instructions for your operating system.

After installation, open your terminal or command prompt and run:

node -v

This command displays your installed Node.js version.

Next, check npm, the package manager commonly used to install JavaScript dependencies:

npm -v

If both commands display version numbers, Node.js and npm are available in your terminal.

Important: If the commands are not recognized, restart your terminal after installation. If the problem continues, check that Node.js was installed correctly and added to your system's PATH.

3. Install a Code Editor

You can write React code in any text editor, but a dedicated code editor makes development easier.

A popular choice is Visual Studio Code.

Download it from:

https://code.visualstudio.com/

VS Code provides useful features such as syntax highlighting, autocomplete, integrated terminal access, and debugging support.

Step 1: Create Your First React Project

There are several ways to start a React application. For this beginner tutorial, we will use Vite, a modern frontend development tool that provides a quick development server and project build system.

Vite is a good choice for learning React because it offers a straightforward setup and fast development experience.

Open your terminal and run:

npm create vite@latest my-first-react-app -- --template react

Let's understand this command:

  • npm create vite@latest runs the latest Vite project generator.

  • my-first-react-app is the name of your project folder.

  • --template react creates a project configured for React.

  • The additional -- passes the template option to the project generator.

You can replace my-first-react-app with another project name if you prefer.

If the command asks for permission to install the project generator, confirm the prompt.

Alternative: Create the Project Interactively

You can also start the project generator without specifying a template:

npm create vite@latest

Follow the prompts to:

  1. Enter your project name.

  2. Select a framework.

  3. Choose React.

  4. Select JavaScript if you are following this tutorial.

Vite may offer additional template options depending on its current version.

Step 2: Navigate to Your Project Folder

After the project has been created, move into its directory:

cd my-first-react-app

This command changes your terminal's current directory to your React project.

You can check the contents of the folder using:

ls

On Windows Command Prompt, you can use:

dir

You should see files and folders similar to these:

my-first-react-app/
├── public/
├── src/
│   ├── assets/
│   ├── App.jsx
│   ├── App.css
│   ├── index.css
│   └── main.jsx
├── index.html
├── package.json
└── vite.config.js

The exact structure can vary slightly depending on the Vite version and template.

Step 3: Install Project Dependencies

Before running the application, install its dependencies.

Execute:

npm install

This command reads the project's package.json file and installs the required packages.

For a newly created React project, these typically include React, React DOM, Vite, and the necessary development tools.

After installation, npm creates a node_modules directory containing the installed dependencies.

You should generally not edit files inside node_modules directly.

Step 4: Start the React Development Server

Now you are ready to run your first React application.

Execute:

npm run dev

Vite starts a local development server and displays a URL in your terminal. It commonly looks like this:

VITE ready

Local: http://localhost:5173/

Open the displayed URL in your browser.

You should see the default Vite and React starter page.

Congratulations! Your first React application is running.

Keep the terminal open while developing. To stop the development server, press Ctrl + C in the terminal.

Step 5: Understand the React Project Structure

Before changing the application, let's understand the most important files.

src/main.jsx

This is the entry point for the React application.

A typical version looks like this:

import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.jsx';
import './index.css';

createRoot(document.getElementById('root')).render(
  <StrictMode>
    <App />
  </StrictMode>
);

Here is what each part does:

  • StrictMode helps identify certain problems during development.

  • createRoot() creates a React root for rendering the application.

  • App imports the main application component.

  • index.css imports global styles.

  • document.getElementById('root') finds the HTML element where React will render the interface.

The StrictMode wrapper is optional, but it is commonly included in new React projects.

src/App.jsx

This file contains the main application component.

You will write much of your initial React code here.

For example:

function App() {
  return (
    <div>
      <h1>Hello, React!</h1>
      <p>Welcome to my first React application.</p>
    </div>
  );
}

export default App;

The App function returns JSX, a syntax extension that allows you to describe UI elements using a familiar HTML-like structure.

src/index.css

This file contains global CSS styles for your application.

You can use it to define default fonts, page spacing, colors, and other styles that apply across the interface.

index.html

This file contains the initial HTML document.

A Vite React project typically includes an element like this:

<div id="root"></div>

React renders your application inside this element.

package.json

This file stores project metadata, scripts, and dependency information.

For example, the scripts section might look like this:

{
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  }
}

The exact file contains additional information, but these scripts are especially useful:

  • npm run dev starts the development server.

  • npm run build creates a production build.

  • npm run preview serves the production build locally for testing.

Step 6: Build Your First React Application

Now let's replace the default starter interface with a simple welcome page.

We will create a small application that displays a heading, a description, and a button that responds to user interaction.

Open src/App.jsx and replace its contents with the following code:

import { useState } from 'react';
import './App.css';

function App() {
  const [count, setCount] = useState(0);

  return (
    <main className="container">
      <h1>Welcome to React!</h1>

      <p>
        You have successfully created your first React application.
      </p>

      <div className="counter">
        <p>You clicked the button {count} times.</p>

        <button onClick={() => setCount(count + 1)}>
          Click Me
        </button>
      </div>
    </main>
  );
}

export default App;

Let's understand the code.

1. Import useState

import { useState } from 'react';

useState is a React Hook that allows a component to store and update state.

In this example, the state represents the number of times the button has been clicked.

2. Create a Component

function App() {
  // Component code
}

App is a React function component. It describes the interface that React should render.

3. Declare State

const [count, setCount] = useState(0);

This statement creates a state variable named count with an initial value of 0.

  • count contains the current value.

  • setCount updates the state.

  • useState(0) sets the initial value to zero.

When you call setCount, React updates the state and renders the component again as needed.

4. Handle Button Clicks

<button onClick={() => setCount(count + 1)}>
  Click Me
</button>

The onClick event handler runs when the user clicks the button.

Each click increases the counter by one.

For a counter that may receive multiple state updates in quick succession, you can use the functional update form:

<button onClick={() => setCount((previousCount) => previousCount + 1)}>
  Click Me
</button>

This form calculates the next value from the previous state.

Step 7: Add CSS to Style Your Application

Your application works, but let's make it look better.

Open src/App.css and replace its contents with:

.container {
  max-width: 700px;
  margin: 80px auto;
  padding: 32px;
  text-align: center;
  font-family: Arial, sans-serif;
}

.container h1 {
  margin-bottom: 16px;
  color: #2563eb;
  font-size: 2.5rem;
}

.container p {
  color: #4b5563;
  font-size: 1.1rem;
  line-height: 1.7;
}

.counter {
  margin-top: 32px;
}

.counter button {
  padding: 12px 24px;
  border: none;
  border-radius: 8px;
  background-color: #2563eb;
  color: white;
  font-size: 1rem;
  cursor: pointer;
}

.counter button:hover {
  background-color: #1d4ed8;
}

Save the file and return to your browser.

Vite's development server supports hot module replacement, so changes to your code will generally appear without manually restarting the server.

Your application should now display a styled heading, a description, and an interactive counter.

You have now created a basic interactive React application!

Step 8: Run a Production Build

During development, you use the Vite development server. Before deploying your application, you should also test a production build.

Run:

npm run build

Vite compiles the application and generates production-ready files, usually in a directory named dist.

If the build completes successfully, you can preview it locally:

npm run preview

Vite displays a local URL where you can inspect the production build.

Remember that the preview server is intended for local verification. Hosting the application publicly requires a deployment platform or web server.

Common React Installation Errors and Their Solutions

Beginners sometimes encounter errors while setting up React. Here are a few common problems and ways to resolve them.

1. node Is Not Recognized

You might see an error indicating that node or npm is not recognized as a command.

Possible cause: Node.js is not installed correctly or its installation directory is missing from your system's PATH.

Solution:

  1. Install the LTS version of Node.js from the official website.

  2. Restart your terminal.

  3. Run node -v and npm -v again.

  4. If the problem continues, verify your PATH configuration.

2. The Project Generator Fails

If the Vite project creation command fails, first check that your Node.js version meets the requirements of the Vite version you are using.

You can check the installed version with:

node -v

If your version is too old, install a supported Node.js version and retry.

You can also review the current setup instructions in the official Vite documentation:

https://vite.dev/guide/

3. The Development Server Does Not Start

If npm run dev fails, confirm that you are inside the project directory.

Run:

cd my-first-react-app
npm install
npm run dev

If the default port is already being used, Vite may display a different local URL. Open the URL shown in your terminal.

4. Changes Do Not Appear in the Browser

Try these steps:

  • Save your file.

  • Check the terminal for compilation errors.

  • Check the browser console for JavaScript errors.

  • Confirm that you edited the correct component.

  • Refresh the browser if necessary.

5. The Application Shows a Blank Page

A blank page can result from a syntax error, an incorrect import path, or a runtime exception.

Open your browser's developer tools and inspect the Console tab.

Also, verify that src/main.jsx imports the correct component and that index.html contains the expected root element.

Best Practices for Beginners Learning React

Once your first application works, follow these practices as you continue learning.

1. Learn Components and Props

Components allow you to divide a user interface into smaller pieces.

Props allow a parent component to pass data to a child component.

For example:

function WelcomeMessage({ name }) {
  return <h2>Welcome, {name}!</h2>;
}

function App() {
  return (
    <div>
      <WelcomeMessage name="Alex" />
      <WelcomeMessage name="Sam" />
    </div>
  );
}

export default App;

Here, WelcomeMessage is reusable. You can display different messages by passing different name values.

2. Understand State and Events

Learn how state works with Hooks such as useState.

Practice building small interactive features, including:

  • Counters

  • Toggle buttons

  • Forms

  • Search filters

  • Expandable menus

These projects help you understand how React responds to user interaction.

3. Keep Components Small

Avoid placing an entire application inside one huge component.

Instead, separate reusable interface elements into their own components when doing so improves readability and maintainability.

4. Use Meaningful Names

Choose descriptive names for components, variables, functions, and files.

For example, ProductCard is more informative than Box, and handleSubmit is more descriptive than run.

5. Read the Official Documentation

The official React documentation is an excellent resource for learning components, JSX, props, state, Hooks, and other concepts.

You can start here:

https://react.dev/learn

Frequently Asked Questions

1. Is React a Framework or a Library?

React is a JavaScript library primarily used to build user interfaces. Developers can combine it with additional tools and libraries to implement routing, data fetching, and other application features.

2. Do I Need to Install React Separately?

You do not normally need to install React globally. When you create a React project using Vite, the project configuration includes React dependencies. Running npm install installs the packages needed by that project.

3. Can I Learn React Without Knowing JavaScript?

You can begin exploring React with basic JavaScript knowledge, but understanding variables, functions, arrays, objects, modules, and modern JavaScript syntax will make React much easier to learn.

4. Why Are We Using Vite Instead of Create React App?

Vite provides a fast development environment and a straightforward way to create a React project.

Create React App is deprecated for new applications, so Vite is a suitable option for this beginner tutorial. Depending on your project requirements, the official React documentation also recommends considering frameworks for applications that need integrated routing, data loading, and server-rendering capabilities.

5. How Do I Start My React Project Again Later?

Open your terminal, navigate to the project directory, and run:

cd my-first-react-app
npm run dev

Open the local URL displayed in your terminal.

You generally need to run npm install again only when setting up dependencies, such as after cloning the project or when required packages are missing.

6. Can I Build a Full Website With React?

Yes. React can be used to build websites, dashboards, admin panels, e-commerce interfaces, and many other interactive applications.

Larger applications may also use routing libraries, backend APIs, databases, authentication systems, and other technologies.

7. Is React Free to Use?

Yes. React is open-source and can be used to develop personal, educational, and commercial projects under its published license.

Conclusion

Installing React and creating your first project is straightforward once you understand the required tools and commands.

In this tutorial, you learned how to install Node.js, create a React project using Vite, install dependencies, start the development server, understand the project structure, and build an interactive counter application.

You also learned how to style the interface, create a production build, troubleshoot common installation problems, and follow React development best practices.

The best way to improve your React skills is to build small projects and gradually add more functionality.

Start with a counter, then try a to-do list, weather application, expense tracker, or simple product listing page. As you gain experience with components, props, state, and Hooks, you will be ready to build more advanced applications.

Your React journey starts with one project. Keep practicing, experiment with new ideas, and build something useful!

D
DevByteAuthor

Technical author and software engineer at DevByte. Writing deep-dive tutorials on distributed systems, modern full-stack web architectures, and cloud deployments.

Keep Exploring

Related Engineering Dispatches

More guides and architectural patterns you might find valuable.

Browse All Articles↘