In this article, we would like to show you how to specify a port to run React app.
Below we present two approaches:
- Specify port by modifying
- Specify port by setting the environment variable.
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"
"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:
- Create a new file in the root of your project and call it
PORTvariable and assign any value you want to it.
Don't forget to add
.gitignoreto prevent people from finding out some of your secret data or just don't store it inside