Environment variable undefined in GatsbyJS?

in Cloud & Serverless


If you're trying to use an environment variable in your GatsbyJS HTML code and it keeps being undefined once you build, this could be because most environment variables are prevented from being rendered e.g. so that your private API key is not revealed by accident.

If your variable does have to end up in the HTML, make sure to prefix it's name with GATSBY_

For example,

  • define as GATSBY_BING_MARKET="en-GB"
  • use as data-src={"https://ui.customsearch.ai/api/ux/rendering-js?market="+process.env.GATSBY_BING_MARKET}
#gatsbyjs