Running the React Development Server
1. Introduction
A React development server is a tool that runs a React application locally during development. It allows developers to open their application in a browser, test features, identify errors, and see changes while writing code.
In a typical React project created with Vite, the development server starts using the npm run dev command. Vite serves the application locally and provides features such as Hot Module Replacement (HMR), which can update the browser when source files change.
The development server is mainly used while building and testing an application. It is different from deploying a finished application to a production hosting environment.
2. What Is a React Development Server?
A React development server provides a local environment for running and testing a React application. It serves the application's HTML, JavaScript, CSS, and other required assets to the browser.
For example, when you start a Vite React project, the terminal may display a local address such as http://localhost:5173/. Opening this address in a browser displays your application.
Important terms:
- Development server: A local server used while developing and testing an application.
- Localhost: A hostname that refers to the current computer.
- Port: A number used to identify a particular network service, such as port 5173.
- Hot Module Replacement (HMR): A feature that updates changed modules during development without requiring a full page reload in many cases.
- Terminal: The command-line interface used to run commands and view server messages.
3. Why Do We Need a Development Server?
- To run and view React applications in a browser.
- To test components, layouts, buttons, forms, and interactive features.
- To detect syntax errors and other development problems.
- To view changes while editing application files.
- To serve JavaScript modules and process development resources.
- To test responsive layouts at different browser sizes.
- To debug application behavior using browser developer tools.
- To check whether the application works before building it for production.
4. Prerequisites for Running a React Development Server
Before starting the server, make sure you have the following:
- Node.js: Provides the JavaScript runtime required by many React development tools.
- npm: The package manager commonly installed with Node.js.
- React project: A project created using a tool such as Vite or an existing React starter project.
- Code editor: An editor such as Visual Studio Code for editing project files.
- Web browser: A modern browser for viewing and testing the application.
- Terminal: A terminal window for entering commands and checking output.
Check whether Node.js and npm are installed by running these commands:
node -v
npm -v
If both commands display version numbers, the tools are available in your terminal. If a command is not recognized, install Node.js or correct your system's PATH configuration.
Check the current Vite documentation for the Node.js version required by your installed Vite release.
5. Creating a React Project Using Vite
If you do not already have a React project, you can create one using Vite.
Step 1: Open the Terminal
Open a terminal in the directory where you want to create the project.
Step 2: Create the Project
npm create vite@latest my-react-app -- --template react
This command creates a project named my-react-app using the Vite React JavaScript template.
Step 3: Enter the Project Directory
cd my-react-app
The cd command changes the terminal's current directory to the project folder.
Step 4: Install Dependencies
npm install
This command installs the packages listed in the project's package.json file, including the dependencies needed to run the development server.
Step 5: Start the Development Server
npm run dev
Vite starts the local development server. Wait until the terminal displays the local URL before opening the application.
6. Understanding the npm run dev Command
The command npm run dev runs the script named dev in package.json.
A typical Vite project contains scripts similar to these:
{
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
}
}
Explanation:
npm run dev starts the development server.
npm run build creates a production build of the application.
npm run preview locally previews the generated production build.
The commands perform different tasks. The development server is for development, the build command generates production assets, and the preview command lets you test the built output locally.
7. Opening the React Application in a Browser
After running npm run dev, the terminal displays server information. A typical output may look like this:
VITE ready
Local: http://localhost:5173/
Network: use --host to expose
press h + enter to show help
The exact output and port can vary depending on your Vite version and configuration.
Follow these steps:
- Run
npm run dev in the project directory.
- Wait for the server to report that it is ready.
- Copy the local URL displayed in the terminal.
- Open your browser.
- Paste the URL into the address bar and press Enter.
- Verify that the React application appears.
Example URL: http://localhost:5173/
Use the URL displayed by your own terminal rather than assuming that every project uses the same port.
8. Understanding Localhost and Port Numbers
When you run a development server, you normally access it through a local URL.
Example: http://localhost:5173/
| Part |
Meaning |
http:// |
The protocol used to communicate with the local server. |
localhost |
Refers to the computer on which the server is running. |
5173 |
The port number used by the development server in this example. |
/ |
The root path of the application. |
The port can change if the default port is unavailable or if you configure a different one. Always check the terminal output for the correct address.
9. Understanding the React Development Workflow
Once the development server is running, you can edit React components and see the results in the browser.
Create or open React project
|
v
Open terminal in project folder
|
v
Run npm install if needed
|
v
Run npm run dev
|
v
Wait for server-ready message
|
v
Open the displayed local URL
|
v
Edit App.jsx or CSS files
|
v
View updates and test the application
This workflow helps developers build and test their application incrementally.
10. Editing App.jsx While the Server Is Running
When the server is running, open src/App.jsx and change the component's content.
Example App.jsx:
function App() {
return (
<main>
<h1>Welcome to React JS</h1>
<p>My development server is running successfully.</p>
</main>
);
}
export default App;
Save the file and return to the browser. Vite usually updates the page automatically when you modify application source files.
If the changes do not appear, check the terminal for errors, confirm that you are editing the correct project, and refresh the browser if necessary.
11. Hot Module Replacement (HMR)
Hot Module Replacement, commonly called HMR, is a development feature that allows supported modules to be updated without performing a complete page reload in many situations.
Benefits of HMR:
- Provides faster feedback while writing code.
- Reduces the need to refresh the browser manually.
- Can preserve component state when supported by the update path.
- Makes UI styling and component adjustments easier to test.
- Improves the development experience.
For example, changing a heading in App.jsx or a color in a CSS file may update the browser shortly after the file is saved.
Note: HMR does not guarantee that every change preserves state. Some changes can trigger a full page reload.
12. Running the Development Server on a Different Port
If you need to use a particular port, Vite supports command-line options.
Example: Use Port 3000
npm run dev -- --port 3000
This passes the --port 3000 option to Vite through the npm script.
If the port is already occupied, Vite may try another available port by default. To make Vite exit instead of selecting a different port, use the strict port option:
npm run dev -- --port 3000 --strictPort
If port 3000 is unavailable, the server exits with an error rather than switching ports.
13. Automatically Opening the Browser
Vite can open the application in a browser when the development server starts.
npm run dev -- --open
You can also specify a path to open, depending on your project and Vite configuration.
This option is useful when you frequently restart the development server and want to open the local application automatically.
14. Accessing the Server from Another Device
By default, the development server is generally intended for local access. If you want to test the application from another device on the same network, you may need to expose the server on a network interface.
npm run dev -- --host
After starting the server, check the terminal for the network address that Vite displays. Open that address from the other device, provided both devices can reach each other through the network and firewall settings allow the connection.
Security note: Exposing a development server can make your source code and development environment accessible to other devices. Only expose it on a trusted network and avoid exposing it publicly unless you understand the security implications.
15. Stopping the Development Server
When you finish testing the application, you can stop the running server from the terminal.
- Return to the terminal window where Vite is running.
- Press
Ctrl + C on Windows, Linux, or macOS terminals that use this shortcut.
- Wait for the process to stop.
Once stopped, the local URL will no longer serve the application through that development server. To continue working, run npm run dev again.
16. Common Problems When Running the React Development Server
| Problem |
Possible Reason |
Solution |
npm is not recognized |
Node.js/npm is not installed or PATH is not configured correctly. |
Install Node.js, reopen the terminal, and check node -v and npm -v. |
| Missing dependencies |
Packages have not been installed. |
Run npm install in the project directory. |
Missing script: dev |
package.json does not define a dev script or the wrong directory is open. |
Check package.json and confirm the project directory. |
| Port already in use |
Another process is using the selected port. |
Stop the conflicting process or run Vite with another port. |
| Blank browser page |
The application has a JavaScript error or its entry point is misconfigured. |
Check the browser console, terminal output, index.html, and main.jsx. |
| Page cannot be reached |
The server is stopped or the URL is incorrect. |
Restart the server and use the exact URL displayed in the terminal. |
| Changes are not visible |
The wrong file is being edited, an error prevents updates, or the browser needs refreshing. |
Check the project path, terminal messages, browser console, and reload the page. |
| Dependency installation fails |
Network problems, incompatible Node.js versions, or package conflicts. |
Review the complete error message, check Node.js compatibility, and resolve the reported issue. |
| Network device cannot connect |
The server is bound only to localhost, or the firewall/network blocks access. |
Try npm run dev -- --host on a trusted network and check the displayed network URL. |
17. Troubleshooting Checklist
- Confirm that Node.js and npm are installed.
- Open the terminal in the correct React project folder.
- Check that package.json exists.
- Run
npm install if dependencies are missing.
- Check that package.json contains the appropriate development script.
- Run
npm run dev.
- Wait for the ready message before opening the browser.
- Use the exact local URL shown in the terminal.
- Check terminal and browser console errors.
- Verify that index.html references the correct entry file.
- Stop the server with
Ctrl + C when finished.
18. Development Server vs Production Build
| Development Server |
Production Build |
| Used while developing and testing the application. |
Creates optimized assets intended for deployment. |
Started with npm run dev in a typical Vite project. |
Generated with npm run build. |
| Provides development features such as HMR. |
Produces files that can be deployed to a suitable hosting service. |
| Usually accessed through a local development URL. |
Served from a production web server or hosting platform. |
| Not intended to be the production hosting solution. |
Designed to provide the application's deployable output. |
After creating the production build, you can test it locally using:
npm run build
npm run preview
The preview server is useful for checking the built output before deployment. It is not intended to replace a production hosting server.
19. Best Practices for Running the React Development Server
- Run commands from the correct project directory.
- Install dependencies before starting a newly downloaded project.
- Read terminal output rather than assuming the default port.
- Keep the terminal open while the development server is running.
- Use the browser console to investigate frontend errors.
- Use HMR to test changes quickly, but verify behavior after major changes.
- Do not confuse the development server with the production build.
- Check Node.js requirements for your installed Vite version.
- Do not expose the development server to untrusted networks.
- Run a production build before deploying the application.
20. Mini Practical Exercise
In this exercise, you will start a React development server, edit the application, and verify that the browser updates.
Step 1: Create a Project
npm create vite@latest react-server-practice -- --template react
Step 2: Enter the Project Folder
cd react-server-practice
Step 3: Install Dependencies
npm install
Step 4: Start the Server
npm run dev
Step 5: Update App.jsx
function App() {
return (
<main>
<h1>React Development Server</h1>
<p>The application is running successfully.</p>
<button onClick={() => alert("React is working!")}>
Test Button
</button>
</main>
);
}
export default App;
Step 6: Test in the Browser
Open the local URL printed in the terminal. Confirm that the heading, paragraph, and button appear. Click the button and check that the message appears.
Step 7: Test HMR
Change the heading text in App.jsx, save the file, and observe the browser. Confirm whether the updated heading appears without manually refreshing the page.
Step 8: Stop the Server
Return to the terminal and press Ctrl + C.
Expected outcome: You will understand how to create a React project, install its dependencies, start the local development server, view the application, edit code, and stop the server.
21. Interview Questions and Answers
Q1. What is a React development server?
It is a local server used to run and test a React application during development.
Q2. Which command starts a Vite React development server?
The usual command is npm run dev, provided package.json defines the corresponding script.
Q3. What is localhost?
Localhost refers to the computer on which the development server is running.
Q4. What is a port number?
A port number identifies a particular service on a computer. A Vite development server commonly uses port 5173 by default, but the actual port can vary.
Q5. What is HMR?
Hot Module Replacement updates supported modules during development, often without requiring a complete page reload.
Q6. How do you stop the development server?
Press Ctrl + C in the terminal running the server.
Q7. What should you do if npm run dev fails?
Check the terminal error, verify that you are in the correct project folder, ensure dependencies are installed, and confirm that the dev script exists in package.json.
Q8. How can you use a different port?
Run npm run dev -- --port 3000 to request port 3000 for a Vite project.
Q9. What is the difference between npm run dev and npm run build?
The dev command starts the development server, while the build command generates production assets.
Q10. Can another device access my development server?
It may be possible if the server is configured to listen on a network interface and the network and firewall permit access. Use this feature only on trusted networks.
22. Practice Questions
- What is the purpose of a React development server?
- Which command starts a typical Vite React project?
- Why do you run npm install before starting a new project?
- What does localhost mean?
- Where can you find the local development URL?
- What is the purpose of HMR?
- How can you run Vite on port 3000?
- How do you stop a running development server?
- What is the difference between a development server and a production build?
- Which tools can help you investigate a blank page?
23. Quick Revision Checklist
- Understand the purpose of a development server.
- Know how to check Node.js and npm versions.
- Know how to create a React project using Vite.
- Understand the purpose of npm install.
- Know how to start the server using npm run dev.
- Understand localhost and port numbers.
- Know how to open the app in a browser.
- Understand HMR and live updates.
- Know how to change the port and open the browser automatically.
- Know how to stop the server and troubleshoot common errors.
- Understand the difference between development, build, and preview commands.
24. Conclusion
Running the React development server is a fundamental step in React application development. In a typical Vite project, you install dependencies, run npm run dev, and open the local URL displayed in the terminal. You can then build the interface, test interactions, debug errors, and observe code changes through development features such as HMR.
Practice this workflow regularly to become comfortable with React project setup and frontend development.
25. Learn React JS with JustAcademy
Explore React components, JSX, project setup, application development, and other practical topics through the React JS training course.
JustAcademy React JS Training Course
To enquire about a course demo, visit the link below.
Register for React JS Course Demo
26. Additional Learning Resources