Languages
[Edit]
EN

React - how to specify a port to run app

0 points
Created by:
Wesley
501

In this article, we would like to show you how to specify a port to run React app.

Below we present two approaches:

  1. Specify port by modifying package.json file,
  2. Specify port by setting the environment variable.

1. Modifying package.json

In your React project go to the package.json file and in "scripts": find the following line:

"start": "react-scripts start"

Modify the line depending on the operating system you are running.

Example solution for Windows:

"start": "set PORT=3001 && react-scripts start"

Example solution for Linux:

"start": "PORT=3001 react-scripts start"

or

"start": "export PORT=3001 react-scripts start"

Example solution for both Windows and Linux:

"start": "(set PORT=3006 || export PORT=3006) && react-scripts start"

2. Setting the environment variable 

To specify port by setting the environment variable you need to:

  1. Create a new file in the root of your project and call it .env,
  2. Inside .env file create PORT variable and assign any value you want to it.
Setting PORT environment variable in .env file
Setting PORT environment variable in .env file

Note:

Don't forget to add *.env into .gitignore to prevent people from finding out some of your secret data or just don't store it inside .env.

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

ReactJS

React - specify a port to run app
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join