Running React Application
Learn how to start the Vite development server, open your React application in a browser, verify changes, and troubleshoot common errors.
1. Introduction
After creating a React project and installing its dependencies, the next step is to run the application. In a Vite-based React project, we usually start the development server using an npm script.
The development server serves the application locally. You can open the provided URL in a browser, edit your React components, and see the updated interface while developing.
2. React Application Execution Flow
This diagram shows the process from starting the development server to viewing your React application.
Terminal
Open the React project folder.
npm run dev
Start the Vite development server.
Vite Development Server
Serves the application on a local URL.
Web Browser
Displays the React user interface.
React Application Running
You can now view and test your application.
3. Before Running the Application
React Project
A React project created with Vite.
Dependencies
Required packages installed using npm.
Terminal
Command Prompt, PowerShell, or the VS Code terminal.
Web Browser
A modern browser to view the application.
4. Step 1: Open the React Project
Open the project folder in Visual Studio Code or navigate to it using a terminal.
For example, if your project is named
my-react-app, run the following command
from its parent directory:
cd my-react-app
Verify Your Current Location
You can use the following command in Windows Command Prompt to display the current directory.
cd
package.json. If you use VS Code, its integrated
terminal may already start in your project folder.
5. Step 2: Install Dependencies
If this is a newly created project and its dependencies have not been installed yet, run:
npm install
npm reads the project's dependency information and installs the packages required by the application.
package.json
Declares dependencies and available npm scripts.
node_modules
Contains the installed project packages.
npm install
every time you start an already configured project.
6. Step 3: Start the Development Server
Run this command in the project directory:
npm run dev
What Happens When You Run This Command?
npm Reads the Script
npm reads the dev script from
package.json.
Vite Starts
The configured development server starts if the command and dependencies are valid.
A Local URL Appears
The terminal shows the URL used to access the app.
VITE ready
Local: http://localhost:5173/
The exact output and port may vary with the installed Vite version and your computer's configuration.
7. Step 4: Open the Application in a Browser
Once Vite displays the local URL, open it in a modern web browser. The default URL commonly uses port 5173, but always use the URL shown in your terminal.
Welcome to React!
Your React application is running.
8. Step 5: Change the React Application
Now open src/App.jsx and replace its contents
with the following example.
function App() {
return (
<div>
<h1>Welcome to React!</h1>
<p>My React application is running successfully.</p>
<button>Explore Courses</button>
</div>
);
}
export default App;
Save the file. Vite normally detects source changes and refreshes the page or applies the update using Hot Module Replacement (HMR).
Expected Browser Output
Welcome to React!
My React application is running successfully.
9. What Is Hot Module Replacement?
Hot Module Replacement (HMR) is a development feature used by Vite to apply supported code changes without requiring a full manual page reload in many cases.
Edit Code
Change a React component.
Vite Detects Changes
Processes the updated module.
UI Updates
Applies supported changes in the browser.
10. How to Stop the Development Server
When you finish testing or need to stop the running server,
switch to the terminal where npm run dev
is running.
Press Ctrl + C in that terminal. If prompted
to terminate the process, respond to the prompt as needed.
11. Common Errors and Solutions
| Problem | Possible Cause | Solution |
|---|---|---|
Missing script: dev |
The current project does not define a dev script, or the wrong folder is open. | Check the current directory and the scripts in package.json. |
vite is not recognized |
Dependencies may not be installed, or the project setup is incomplete. | Run npm install in the project directory. |
| Port already in use | Another process may already be using the default port. | Check the terminal and open the alternate URL Vite provides. |
| Browser cannot connect | The server may have stopped or the wrong URL was opened. | Check the terminal and run npm run dev again if necessary. |
| Blank page | There may be a compile error or a problem in the application code. | Check the terminal, browser console, and React component code. |
| Changes are not visible | The file may not have been saved, or an error may prevent the update. | Save the file and check the terminal and browser console. |
Debugging Tip
Check the terminal first for compilation and server errors. Then inspect the browser console if the application still does not behave as expected.
12. Development Server vs Production Build
The development server is intended for writing and testing code. Before deploying an application, you normally create a production build.
| Development | Production Build |
|---|---|
Uses npm run dev. |
Uses npm run build in a typical Vite project. |
| Designed for coding and testing. | Generates optimized assets for deployment. |
| Usually serves the app from localhost. | Usually creates files in the dist directory. |
Create a Production Build
npm run build
Run this command when you are ready to check the production build. Deployment itself requires an appropriate hosting setup.
13. Practice Task
Task: Run and Modify Your React App
- Open your Vite React project in VS Code.
- Open a terminal in the project directory.
- Run
npm installif dependencies are not installed. - Execute
npm run dev. - Open the local URL displayed in the terminal.
- Update the heading in
src/App.jsx. - Save the file and check the updated browser output.
- Stop the development server with
Ctrl + C.
Summary
- Open the terminal in the correct project directory.
- Run
npm installif dependencies are missing. - Use
npm run devto start the Vite development server. - Open the URL displayed by Vite in the browser.
- Update
src/App.jsxto practise React development. - Vite can apply supported changes through HMR.
- Use
npm run buildto create a production build in a typical Vite project.