create-react-app is supposed to inject your .env
variables into your React app. I have used the REACT_APP_
prefix with my variables in my .env
and .env.development
.
However, when debugging the code I've found that process
itself is undefined. So when trying to access an environment variable with process.env.REACT_APP_SOMETHING_URL
, the root process
variable is undefined.
So at the time I misunderstood how process.env
works in create-react-app
. I expected this to be available at runtime. However, because React is a frontend library and process
is a Node backend entity you cannot directly access process.env
while executing code in the browser.
This makes sense because in-browser executed Javascript doesn't know about Node; therefore, process.env
isn't available.
What happens instead is that during a webpack build, webpack will inject the corresponding environment variables into your frontend asset code. So if you have a production .env
file, those variables are provided during the build.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With