W
Wasp4mo ago
jason

How the hell do I use a value from .env.client?

Hi, I am trying to use a basic env var I've outlined in .env.client. Exactly as the documentation outlines (https://wasp.sh/docs/project/env-vars#client-env-vars):
import { env } from 'wasp/client'
console.log(env.REACT_APP_GOOGLE_MAPS_API_KEY)
import { env } from 'wasp/client'
console.log(env.REACT_APP_GOOGLE_MAPS_API_KEY)
Yet the project fails to compile, saying: error TS2339: Property 'REACT_APP_GOOGLE_MAPS_API_KEY' does not exist on type '{ REACT_APP_API_URL: string; }'. What the hell is it talking about? Why did I follow the documentation exactly yet Wasp act like it has no idea what the env var is?
2 Replies
kapa.ai
kapa.ai4mo ago
Hi @jason. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I understand your frustration with the environment variables in Wasp. There seems to be a discrepancy between the documentation you're referencing and the actual implementation. Based on the knowledge sources, there are two issues to address: 1. The correct way to access client environment variables in Wasp is using import.meta.env rather than import { env } from 'wasp/client'. According to the documentation:
console.log(import.meta.env.REACT_APP_SOME_VAR_NAME)
console.log(import.meta.env.REACT_APP_SOME_VAR_NAME)
2. For accessing the configuration object (which is different from directly accessing env vars), you would use:
import { config } from 'wasp/client'
console.log(config.apiUrl)
import { config } from 'wasp/client'
console.log(config.apiUrl)
The error you're seeing suggests that Wasp only recognizes REACT_APP_API_URL in the config object, but not your custom environment variable. To fix this: 1. Make sure your .env.client file is properly set up with:
REACT_APP_GOOGLE_MAPS_API_KEY=your_api_key_here
REACT_APP_GOOGLE_MAPS_API_KEY=your_api_key_here
2. Access it in your code using:
console.log(import.meta.env.REACT_APP_GOOGLE_MAPS_API_KEY)
console.log(import.meta.env.REACT_APP_GOOGLE_MAPS_API_KEY)
During development, Wasp should pick up the variables from .env.client. For production, you'll need to provide them during the build process:
REACT_APP_GOOGLE_MAPS_API_KEY=your_api_key_here npm run build
REACT_APP_GOOGLE_MAPS_API_KEY=your_api_key_here npm run build
Or when using Wasp deploy:
REACT_APP_GOOGLE_MAPS_API_KEY=your_api_key_here wasp deploy fly deploy
REACT_APP_GOOGLE_MAPS_API_KEY=your_api_key_here wasp deploy fly deploy
Wasp Environment Variables Documentation provides more details on how environment variables work in Wasp. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @jason, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
jason
jasonOP4mo ago
lmao wtf. If I'm supposed to access client env vars via "import.meta.env.REACT_APP..." then wtf is that part in the documentation about? "import { env } from 'wasp/client' console.log(env.REACT_APP_SOME_VAR_NAME)" Why even mention that at all if it's not correct?

Did you find this page helpful?