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
Asked
Active
Viewed 2,978 times
1 Answers
5
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
-
1Thank you - that wouldn't have been very easy to figure out ! – Zach Smith Aug 13 '19 at 18:52