Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create react app default port env

I'm trying to change the default port being used by create react app. If I use "start": "PORT=4000 react-scripts start" and restart the server, it works fine. But using "start": "process.env.REACT_APP_PORT react-scripts start" returns undefined. My .env.development file has REACT_APP_PORT=4000 and I can console log the port from index.js, so it does seem to be set OK at that point

like image 625
Brian Harrison Avatar asked Feb 24 '26 04:02

Brian Harrison


1 Answers

process.env.PORT will start the app on the desired port (current CRA version is 1.1.4 at the time of this response)

Hence in your env file

.env

# Works
  PORT=4000
# Won't Work
  REACT_APP_PORT=4000

This is because PORT is used by webpack (in CRA) at build time and not by React at run time.

A list of variables NOT requiring the REACT_APP prefix can be found here

like image 68
Verric Avatar answered Feb 26 '26 04:02

Verric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!