Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the default port in react js [duplicate]

I could able to start react js with default port 3000. But, when I customised the port to 4200, "start": "PORT=4200 react-scripts start"(Just changed in the package.json), I am not able to start the react js application.

'PORT' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `PORT=4200 react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional lo
ging output above.

npm ERR! A complete log of this run can be found in:
like image 240
java learning Avatar asked Oct 29 '25 09:10

java learning


2 Answers

to start APP on another port run the following npm command

npm start --port 3002

or to config in package use

replace the start command with below

"start": "set PORT=3006 && react-scripts start"
like image 123
Manrah Avatar answered Oct 31 '25 01:10

Manrah


An other option that was not mentioned in the other answer would be to create a .env file in the root directory and put PORT=4200 in it. It will automatically be loaded in your environment variables.

like image 40
Antoine Gagnon Avatar answered Oct 30 '25 23:10

Antoine Gagnon