How to specify a port to run a create-react-app based project ?
Last Updated :
30 Oct, 2023
Usually the react app server runs on the port 3000. But there may be some situations, where user needs to specify a port to run the react app. So, users need to change the default port of the application and sepcify a custom port to run the application.
Prerequisites
Creating React Application
Step 1: Create a new react application running the below command to your terminal.
npx create-react-app testapp
Step 2: Move to the project directory by running the below command to the terminal.
cd testapp
Project structure
It will look like this.

Example: Basic react app running on custom port 5000.
JavaScript
// Filename - App.js
import React, { Component } from "react";
class App extends Component {
render() {
return (
<div>
<h1>GeeksForGeeks</h1>
<h2>
Server is currently running on port 5000
</h2>
</div>
);
}
}
export default App;
Steps to Run the Application: Use this command in the terminal
npm start
Output: This output is visible on http://localhost:5000/ on the browser window

Method 1: Create an environment variable
This is the simplest method to change the default port of the react app. We need to create the .env file inside the project directory and add the environment variable. Users need to add the below code inside the .env file.Â
PORT=<specify_port_of_your_choice>
Example:
PORT=5000
Now, run the project using the npm start command, and react app will automatically start to run on the port of your choice.Â
Method 2: Edit the package.json file
In this method, we have to edit a single line of code inside the package.json file. Here, The user will find the code like "start": "react-scripts start" inside the "scripts" object. In the below image, you can see the default view of the "scripts" object.

Users need to edit the first line of the "scripts" object and they have to add the below code there.
"start": "set PORT=<specify_port_of_your_choice> && react-scripts start"
Example:
"start": "set PORT=5000 && react-scripts start"
After editing the package.json file, your "scripts" object should look like the below image.

Method 3: Install and add cross-env package
First, we need to install the "cross-env" package in the project directory. So, open the terminal and run the below command inside the project directory.
npm i -D cross-env
devDependencies in package.json
{
"devDependencies": {
"cross-env": "^7.0.3"
}
}
After installing the cross-env package, the user needs to edit the first line of the "scripts" object inside the package.json file. Users need to change the below code by removing the first line inside the "Scripts" object.
"start": "cross-env PORT=<specify_port_of_your_choice> react-scripts start"
Example:
"start": "cross-env PORT=5000 react-scripts start"
Your "Scripts" object should look like the below image after making changes inside the code.

Method 4: Specify port with the run command
In this method, We don't need to edit any files inside the react app. We have to just mention the port with the run command of the react project. the user has to use the below command to run the project instead of npm start.
PORT=<specify_port_of_your_choice> npm start
Example:
PORT=5000 npm start
When the user will run the react project using the above command, it will start on the port of the user's choice.
Similar Reads
How to Create a Socket at a Specific Port in Java?
A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes are used to represent the connection between a client program and a server program. Socket Programming, us basically client-server programming where a socket is used as a link betwee
3 min read
How to import from React-Select CDN with React and Babel ?
In ReactJS, import from React-Select CDN is not done in a simple way as we do with the npm packages. To use them we need to use directly them in the HTML file. It does not work with the project set-up by creating a react app. Prerequisites:React JSHTML, CSS, JavaScriptSteps to Import React-Select C
2 min read
How To Create Absolute Imports In Vite React App?
When working on a React app using Vite, we often come across situations where we have a complex folder structure which also makes our component import URLs complex. To resolve this issue we can use Absolute imports in Vite React App. absolute imports help us to import files directly from the root fo
3 min read
How to Download a React Project from Github and Run in My PC ?
Downloading a React project from GitHub and running it on your local machine involves cloning the repository, installing dependencies, and starting the development server. This articles covers a step-by-step guide on how to download a React project from GitHub and run it locally.Prerequisites:NPM
3 min read
Building a react boilerplate from scratch without using create-react-app
In this article, we are going to build a basic boilerplate for a React project from scratch without using the create-react-app or any other predefined boilerplate. This is a great experience for any react developer to look into what is happening behind the scenes.The file structure for our project w
6 min read
How to Create a New Next JS 13+ App?
Creating a new Next.js 13+ application is a straightforward process that sets up a modern React-based framework with built-in features like server-side rendering, static site generation, and API routes. Next JS streamlines and automates the setup of essential React tooling, handling tasks such as bu
2 min read
What are the steps to create first React Native App ?
React Native is an open-source UI software framework created by Meta Platforms, Inc. It is used to develop applications for Android, Android TV, iOS, etc. Weâre always looking for shorter development cycles, quicker time to deployment, and better app performance. And there are so many hybrid mobile
4 min read
How To Secure a Vite-Powered React App?
Securing a Vite-powered React app involves several best practices and strategies to protect your application from threats and vulnerabilities. Vite, a fast and modern frontend build tool, combined with React, provides a robust environment for building web applications, but it's important to ensure t
4 min read
How to run Node/Express on a specific port?
Port in Node is the numerical endpoint that is assigned to the process to allow communication over the network. Each application is identified uniquely by using this port number. There are port numbers that range from 0 to 65535, some of the ports are already assigned to the common services. In this
3 min read
How to Deploy React project on Firebase?
When developing any project we must host it somewhere so that the whole world can see our hard-work. Hosting websites can be hectic sometimes, but you don't need to worry as we can now host our React project on Firebase within a minute or two with a very simple setup. The Steps to deploy react proje
2 min read