Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create-react-app .env: process not defined

Tags:

reactjs

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.

like image 566
Dylan Pierce Avatar asked Oct 04 '18 22:10

Dylan Pierce


1 Answers

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.

like image 80
Dylan Pierce Avatar answered Oct 14 '22 22:10

Dylan Pierce