How to access environment variables in a react component?

Hi,

I had a similar issue with Create React App, so leaving the solution for that here, in case it helps you to troubleshoot.

For Create React App, the solution ended up being that all Create React App environmental variables had to be prepended with REACT_APP_. So, process.env.REACT_APP_KEY would be defined, but process.env.KEY would be undefined.

(I had thought that the prepended part was only necessary for the local environment, but in fact it also was necessary in NODE_ENV=production).

Maybe Gatsby has some similar issue?

2 Likes