Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eshap.k's avatar

Loading .env variables for vue3 and vitejs

I am simply trying to load a tweet and there is a variable called bearer_token that you need in order to get a json object with stuff for tweets. I am trying to store it in a .env file. I stored it in .env.local but it is not pulling the variable.

What should I do? Here is the documentation for using .env in Vitejs .env file

BEARER_TOKEN="MYLONGBORINGRANDOMPASSWORD"

Now, App.vue

<script setup>
const BEARER_TOKEN = import.meta.env.VITE_BEARER_TOKEN;

console.log(BEARER_TOKEN);
</script>

But, it is not working.

0 likes
4 replies
vincent15000's avatar

You are trying to retrieve a variable that doesn't exist in the .env file.

import.meta.env.BEARER_TOKEN instead of import.meta.env.VITE_BEARER_TOKEN

MaverickChan's avatar

you need to specify the .env file directory in the vite.config.js file assuming your .env file is in the src directory

envDir: 'src',

then , you can load your variables

1 like

Please or to participate in this conversation.