EN
React - how to specify a port to run app
0
points
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
package.json
file, - 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:
- Create a new file in the root of your project and call it
.env
, - Inside
.env
file createPORT
variable and assign any value you want to it.
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
.