I have a create-react-app
that is using dotenv to hide a Google Maps API key, but when I try this I'm unable to get the map to load: I get an error saying Google Maps API error: InvalidKeyMapError
. I was able to get this working before, but this was with the API key exposed.
Here's what I have in my files. In App.js
:
import React, { Component } from 'react'
import { GoogleApiWrapper } from 'google-maps-react'
require('dotenv').config()
const apiKey = `${process.env.REACT_APP_GOOGLE_MAPS_KEY}`
console.log(apiKey)
...
export default GoogleApiWrapper({
apiKey: apiKey,
})(App)
In my .env
file I have: REACT_APP_GOOGLE_MAPS_KEY=myapikey
In my app.js file I pass down the google props to my MapContainer
component, which is in my render method, that loads the map itself. Is there some kind of conflict with dotenv and being able to read the google-maps-react
API key?
I've already tried searching for the answer here and came across this: How do I hide API key in create-react-app?
However even after restarting the server multiple times I get back the same error as well as undefined
for the console.log(apiKey)
.