Hello! On this article, I want to share what I have found on changing ports in React. In Express, its easy to declare any port number by assigning it to a constant variable:
In ExpressJS:
import express from 'express';
const app = express();
const PORT = <any number>;
In React:
- Go to
package.json
file.
- Under
scripts
line, modify the command onstart
and save (Ctrl+S
).
"scripts": {
"start": "set PORT=8000 && react-scripts start",
"build": "react-scripts build"
}
- After modifying
start
, go to Terminal (Ctrl+~
) -->npm start
--> hitEnter
(if your app is in another folder, type incd <folder name>
, hitEnter
then type innpm start
--> hitEnter
).
- When viewed in browser, it will now look like this:
Aaaaaand that's it! ๐