3

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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you - that wouldn't have been very easy to figure out !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.