# Environment variables that were set in the UI are undefined

**URL:** <https://answers.netlify.com/t/environment-variables-that-were-set-in-the-ui-are-undefined/45176>\
**Category:** Support\
**Tags:** netlify-cli, environment-vars\
**Created:** [October 5, 2021, 10:41am UTC](https://answers.netlify.com/t/environment-variables-that-were-set-in-the-ui-are-undefined/45176 "2021-10-05T10:41:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abba](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@Abba](https://answers.netlify.com/u/Abba)\
**Post date:** [October 5, 2021, 10:41am UTC](https://answers.netlify.com/t/environment-variables-that-were-set-in-the-ui-are-undefined/45176/1 "2021-10-05T10:41:56Z")

</div>

I have set some environment variables in the UI.

See here:

 ![Screen Shot 2021-10-05 at 9.35.37 pm](https://global.discourse-cdn.com/netlify/original/3X/3/9/391e5c4e0d639490d10638195d6008223fcf0812.png)

I am trying to use them in my code like this:

```auto
    console.log("AUTH0_DOMAIN:");
    console.log(process.env.AUTH0_DOMAIN);
    console.log("AUTH0_CLIENT_ID:");
    console.log(process.env.AUTH0_CLIENT_ID);
    console.log("AUTH0_AUDIENCE:");
    console.log(process.env.AUTH0_AUDIENCE);

```

When starting up the local dev server using “ntl dev” it looks like the environment variables are being injected:

 ![Screen Shot 2021-10-05 at 9.25.42 pm](https://global.discourse-cdn.com/netlify/original/3X/8/6/86a8ab1bdb3b27a06a4b42da2f69c5dda74c6c69.png)

But they all come through as `undefined` as shown here in the console:

![Screen Shot 2021-10-05 at 9.40.43 pm](https://global.discourse-cdn.com/netlify/original/3X/9/9/994bed4043543a302b1693e994f19140e45f94a2.png)

So what am I doing wrong?

Why are they coming through as undefined?

---

<div class="post-metadata">

**Author:** ![tomrutgers](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/tomrutgers/32/107_2.png) [@tomrutgers](https://answers.netlify.com/u/tomrutgers)\
**Post date:** [October 5, 2021, 11:05am UTC](https://answers.netlify.com/t/environment-variables-that-were-set-in-the-ui-are-undefined/45176/2 "2021-10-05T11:05:56Z")

</div>

You shouldn’t use secret keys in your front-end, as they will end up in your code after the build process is completed. If you use secret keys, use Netlify Functions instead.

You can load environment variables within your functions like this:

```auto
process.env.SECRET_KEY

```

---

<div class="post-metadata">

**Author:** ![Abba](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@Abba](https://answers.netlify.com/u/Abba)\
**Post date:** [October 5, 2021, 10:22pm UTC](https://answers.netlify.com/t/environment-variables-that-were-set-in-the-ui-are-undefined/45176/3 "2021-10-05T22:22:27Z")

</div>

Ah good to know! Thanks.

But I’d still like to know why they didn’t get injected on the front end so I can use other environment variables that aren’t secret keys.

Any idea why these didn’t show in my console?

For the front end do I need to use something different from `process.env.VARIABLE`?

---

<div class="post-metadata">

**Author:** ![Abba](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@Abba](https://answers.netlify.com/u/Abba)\
**Post date:** [October 6, 2021, 6:57am UTC](https://answers.netlify.com/t/environment-variables-that-were-set-in-the-ui-are-undefined/45176/4 "2021-10-06T06:57:35Z")

</div>

UPDATE: The environment variables are also undefined after deploying live to Netlify. So it’s broken on the live version and dev version.

---

<div class="post-metadata">

**Author:** ![tomrutgers](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/tomrutgers/32/107_2.png) [@tomrutgers](https://answers.netlify.com/u/tomrutgers)\
**Post date:** [October 6, 2021, 7:34am UTC](https://answers.netlify.com/t/environment-variables-that-were-set-in-the-ui-are-undefined/45176/5 "2021-10-06T07:34:50Z")

</div>

It’s not broken, that’s by design. I’ll try to explain.

Environment variables are injected at **build time**. They’re not actually available after your site is built, since that would require a running server. The exception of course being Netlify Functions, but I’ll keep that out of the equation for now.

So what happens is this:

1. You define an environment variable in the Netlify UI:

```auto
PUBLIC_KEY="asdf1234"

```

1. You then load the environment variable in your javascript file:

```auto
const publicKey = process.env.PUBLIC_KEY

```

1. The environment variable gets injected in your javascript file at build time:

```auto
const publicKey = 'asdf1234'

```

So when you try to log the key to your console **after** your site is built, this is undefined:

```auto
console.log(process.env.PUBLIC_KEY);

#undefined

```

But this isn’t:

```auto
console.log(publicKey);

# asdf1234

```

Hopefully this also explains why it’s a bad idea to use secret keys as build env variables 🙂

---

<div class="post-metadata">

**Author:** ![Abba](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@Abba](https://answers.netlify.com/u/Abba)\
**Post date:** [October 6, 2021, 11:25am UTC](https://answers.netlify.com/t/environment-variables-that-were-set-in-the-ui-are-undefined/45176/6 "2021-10-06T11:25:22Z")

</div>

Hey, thanks for helping me out, but assigning it to a variable also doesn’t work:

```auto
const a_d = process.env.AUTH0_DOMAIN;
console.log(a_d); // This prints undefined

```

---

<div class="post-metadata">

**Author:** ![tomrutgers](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/tomrutgers/32/107_2.png) [@tomrutgers](https://answers.netlify.com/u/tomrutgers)\
**Post date:** [October 6, 2021, 11:58am UTC](https://answers.netlify.com/t/environment-variables-that-were-set-in-the-ui-are-undefined/45176/7 "2021-10-06T11:58:08Z")

</div>

Can you share a repository?

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [October 7, 2021, 10:12am UTC](https://answers.netlify.com/t/environment-variables-that-were-set-in-the-ui-are-undefined/45176/9 "2021-10-07T10:12:17Z")

</div>

I believe for Vue CLI to be able to handle this, you need to prefix the variables with `VUE_APP_`:

[Modes and Environment Variables | Vue CLI](https://cli.vuejs.org/guide/mode-and-env.html#using-env-variables-in-client-side-code).

So yours could be: `process.env.VUE_APP_AUTH0_DOMAIN`. The name would have to be changed in the UI too.

---

<div class="post-metadata">

**Author:** ![Abba](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@Abba](https://answers.netlify.com/u/Abba)\
**Post date:** [October 7, 2021, 9:57pm UTC](https://answers.netlify.com/t/environment-variables-that-were-set-in-the-ui-are-undefined/45176/10 "2021-10-07T21:57:58Z")

</div>

OH MY GOD THANK YOU!

That worked. Been pulling my hair out for a few days now.
