Quickly answer
In CRA you can use environment variables declaring them before the npm run start
You must create custom environment variables beginning with REACT_APP_
. Example:
export REACT_APP_DISABLE_NEXT = true
CRA framework will expose this operative system level variable to a simple javascript variable ready to use in any part of your react code:
process.env.REACT_APP_NOT_SECRET_CODE
source:
Explanation
Environment variables like export FOO=BAR
are variables at operative system level. These variables are easy accessed with:
process.env.FOO
React is not a nodejs javascript. React will be converted to vanilla javascript to be able to run in a browser. In this javascript process.env does not exist.
But CRA Framework (which is nodejs), have access to the operative system variables, so it create us an emulation of process.env containing just variables which start with REACT_APP_
. Any other variable without REACT_APP_
, will be ignored
Advice
- Expose
export REACT_APP_DISABLE_NEXT = true
just in dev or test stage, not in prod