Search code examples
environment-variablesvitenetlify

Vite app deploy in Netlify - environment variable is missing or empty?


I'm struggling to understand how to use Netlify environment variables in my Vite app deployment. I get this error

enter image description here

The API key is stored in Netlify here:

enter image description here

And my code is here:

enter image description here

I've read Netlify's docs, search the internet and tried AI. I just can't figure it out.

Can anyone save me?

Thanks

Rich


Solution

  • Your first and second images show the problem: your build is looking for $OPENAI_API_KEY and you've set $VITE_APP_OPENAI instead. The names need to match exactly :)

    One trick I like to use to debug environment variables is to prepend my build command with env && since that will list all variables before starting your build (though not before dependency installation, in case you need that to install npm modules during the first stage of the Netlify build).