Lesson 10 of 60 – Running a React App
17%

Running a React App

After creating a React project, the next step is to run the application and open it in a browser. In this lesson, you will learn how to start a React development server, open your application, understand the terminal output, and stop the development server.

Note: This lesson uses the common Vite-based React workflow created with npm create vite@latest.

1. What Does Running a React App Mean?

Running a React app means starting a development server that builds and serves your React application so that you can view it in a web browser.

React Project
     ↓
Development Server
     ↓
Web Browser
     ↓
React Application

2. Open the Project Folder

Before running the application, open the terminal and move into your React project folder.

cd my-react-app

The terminal should be working inside the folder that contains package.json.

3. Check package.json

The package.json file contains the scripts that can be used to run the project.

{
    "scripts": {
        "dev": "vite"
    }
}

The exact scripts may vary depending on how the project was created.

4. Install Dependencies

If the project dependencies have not been installed yet, run:

npm install

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

5. Start the Development Server

For a typical Vite React project, start the development server using:

npm run dev

This command runs the development script defined in package.json.

6. Understanding npm run dev

The command can be understood as follows:

npm
 ↓
Run a script
 ↓
dev
 ↓
Start the development server

The dev script commonly starts Vite in a Vite-based React project.

7. Terminal Output

After running npm run dev, the terminal normally displays information about the development server.

VITE vX.X.X  ready in XXX ms

➜  Local:   http://localhost:5173/

The exact version and startup time may be different on your computer.

8. Localhost

localhost refers to your own computer. A development server can make your React application available through a local URL.

http://localhost:5173/

The port number can be different depending on the development server configuration.

9. Open the React App in a Browser

Copy the local URL shown in the terminal and open it in a web browser.

http://localhost:5173/

You should then see the React application running in the browser.

10. Browser and Development Server

The browser sends requests to the local development server, and the development server provides the application files.

Browser
   ↓
localhost
   ↓
Development Server
   ↓
React Project

11. Editing App.jsx

Open src/App.jsx and change the displayed content.

function App() {

    return (
        <h1>Welcome to My React App</h1>
    );

}

export default App;

Save the file after making the change.

12. Hot Module Replacement

During development, Vite can update the application in the browser when source files are changed. This development feature is commonly called Hot Module Replacement (HMR).

Edit Code
    ↓
Save File
    ↓
Development Server
    ↓
Browser Updates

13. Why HMR Is Useful

HMR makes development faster because you can make changes and quickly see the result without manually rebuilding the entire application after every small change.

Write Code
    ↓
Save
    ↓
See Changes
    ↓
Continue Development

14. Running the App from VS Code

You can use the integrated terminal in Visual Studio Code to run your React application.

VS Code
   ↓
Terminal
   ↓
cd my-react-app
   ↓
npm run dev

The terminal can remain open while you work on the project.

15. Open the Integrated Terminal

In VS Code, you can open the integrated terminal from the Terminal menu.

After opening the terminal, make sure you are inside the React project directory.

cd my-react-app

16. Start Command Again

Once the terminal is inside the project folder, run:

npm run dev

The development server will start and provide a local address.

17. Changing the Port

Sometimes the default port is already being used by another application. In that situation, the development server may use another available port.

Local: http://localhost:5174/

Always use the URL displayed by your terminal.

18. Port Already in Use

If a port is already being used, the development tool may report the issue or select another available port depending on its configuration.

The important point is to check the terminal output instead of assuming that the application must always use one specific port.

19. Checking the Browser

After opening the local URL, check whether your React application is displayed correctly.

  • Check the page title.
  • Check the headings.
  • Check buttons and links.
  • Check component output.
  • Check the browser console for errors if necessary.

20. Browser Developer Console

The browser developer tools can help you find JavaScript errors and other problems.

Browser
   ↓
Developer Tools
   ↓
Console
   ↓
Check Errors

Console messages can provide useful information when an application does not behave as expected.

21. React Development Cycle

Start Server
     ↓
Open Browser
     ↓
Write React Code
     ↓
Save Code
     ↓
Browser Updates
     ↓
Test
     ↓
Repeat

This cycle is used frequently while developing React applications.

22. Stopping the Development Server

When you want to stop the development server, focus on the terminal where it is running and press:

Ctrl + C

This stops the currently running development process.

23. Starting the Server Again

After stopping the server, you can start it again whenever you need it.

npm run dev

Then open the local URL displayed by the terminal.

24. Common Error: npm Not Found

If the terminal says that npm is not recognized, Node.js may not be installed correctly or the system PATH may not be configured correctly.

Check Node.js and npm using:

node --version
npm --version

25. Common Error: Wrong Folder

If npm run dev cannot find the required project script, make sure the terminal is inside the correct project folder.

cd my-react-app
npm run dev

The project folder normally contains package.json.

26. Common Error: Dependencies Missing

If dependencies have not been installed, run:

npm install

Then try starting the development server again:

npm run dev

27. Recommended Running Workflow

1. Open VS Code
2. Open React Project
3. Open Terminal
4. Check Project Folder
5. Run npm install if needed
6. Run npm run dev
7. Open Local URL
8. Edit src files
9. Test in Browser
10. Stop with Ctrl + C

28. Development Server vs Production

The development server is mainly used while developing and testing the application.

Development
     ↓
npm run dev
     ↓
Local Testing

A production deployment uses a production build process appropriate to the project's setup.

29. Useful Commands

# Check Node.js
node --version

# Check npm
npm --version

# Install dependencies
npm install

# Start development server
npm run dev

# Stop development server
Ctrl + C

These commands are commonly used during React development.

30. Complete React Running Process

React Project
     ↓
Open Terminal
     ↓
cd my-react-app
     ↓
npm install
     ↓
npm run dev
     ↓
Development Server
     ↓
Open localhost URL
     ↓
React App in Browser
     ↓
Edit src Files
     ↓
Save Changes
     ↓
Test Application
     ↓
Ctrl + C to Stop

Once you understand this process, you can easily start and test your React applications during development.

📌 Key Points

  • Use the terminal to run your React application.
  • Make sure the terminal is inside the correct project folder.
  • Use npm install when project dependencies need to be installed.
  • Use npm run dev to start a typical Vite development server.
  • Open the local URL displayed by the terminal in your browser.
  • The port number can vary depending on the project and available ports.
  • HMR can update the browser during development when source files change.
  • Use browser developer tools to investigate client-side errors.
  • Use Ctrl + C to stop the development server.
  • Use the terminal output as the source of the actual local URL.

🧠 Quick Quiz

Question: Which command is commonly used to start a Vite React development server?