I'd like to start by saying I followed all of the suggestions here How do I hide API key in create-react-app? but none of them worked. I've searched for over an hour trying to find an answer but nothing. Below is my explanation.
- I used
create-react-appto scaffold my project - I'm using Google Maps and have not used any other API's yet
- I created a
.envfile in the root of my project - In that file I added
REACT_APP_GOOGLE_MAPS_API_KEY = api key - In my component file
Map.jsI addedconst GM_API_KEY =${process.env.REACT_APP_GOOGLE_MAPS_API_KEY};(the tick marks are around this variable in my file but they won't show up here) - In my url for the map, I added the key like so
"https://maps.googleapis.com/maps/api/js?key=GM_API_KEY&callback=initMap" - Also, at the top of my
Map.jsfile I added aconsole.log(GM_API_KEY);to ensure it was working. - I exported my Map component and then imported it in my
App.js - I restarted my server
When the page loads, my key is output in the console, but I still get an error from Google saying my API key is invalid. Also, if I manually console.log(GM_API_KEY) after the page has loaded I get a reference error saying that GM_API_KEY is undefined
If anyone has any suggestions or can provide any help I would greatly appreciate it! Thank you for taking time to check out my question.