How to change the default port for your react app

Search for a command to run...

No comments yet. Be the first to comment.
Ever stumbled upon the term Web5 and wondered, "What on earth could that be?" Well, today's your lucky day, because we're about to unravel this mystery together! Web5, in its essence, stands for the fifth iteration of the web, a revolutionary concep...

Introduction Discover the magic behind lightning-fast frontend frameworks that revolutionize the web development landscape! Unravel the secrets of Next.js, Vite, Astro, SvelteKit, Nuxtjs, and Qwik, as we dive deep into what makes them stand out in th...

Photo by Rahul Mishra Unsplash In this article, I'm going to reveal some super amazing React application tips that will totally boost your performance and elevate your code-writing skills! I'll be sharing all the cool stuff I know and use, but if you...

Are you ready to say goodbye to an old friend? For many React developers, create-react-app has been a trusty companion in their journey of building single-page applications. It's made setting up projects a breeze and saved countless hours of configur...

Introduction We have all encountered errors while building our react application. Errors that will always break our application. And then, when we check our console for the type of error, we see something like this How do we Deal with Ugly Errors Li...

There are several ways to change the default port for your react application, Let us start with the easy one.
You can create a .env file and just add the port number you want to use. Like this
PORT=4000
// .env file
PORT=4000
Once you set it, you can start your react application. React will automatically start the application using the PORT number you specified.
You can also change your port number from your package.json file. Let us see how.
Add PORT=3456 to the value of start on MacOS and set PORT=4000 to the value of start on Windows.
On macOS
"scripts": {
"start": "port=4000 react-scripts start",
},
On Windows
"scripts": {
"start": "set port=4000 && react-scripts start",
},
Using this method, you are changing the port number using your OS's environmental variable.
// Using cmd
set PORT=4000
// using terminal, or bash
export PORT=4000
// using powershell
$env:PORT=4000
After running this on your terminal or cmd, run npm start to start the project
The cross-env package can also change your port number to a custom port. First, to be able to change our port number using this package, we will have to install, it.
On your terminal or command prompt, run npm install cross-env Once it is installed add cross-env PORT=4000 to the value of start in your package.json
"scripts": {
"start": "cross-env PORT=4000 react-scripts start",
},
After doing any of the above methods, your react app should be available on http://localhost:4000