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

CamKem's avatar
Level 10

How to call .env variavbles in Vue3 files

Hello,

I am trying to work out how to call the variables that live in the .env file from inside of a Vue file in a laravel project. Surely there is an easy way to do this I am overlooking...

Using the follow doesn't''t work but I didn't expect it would.

<template>
<Head title="{{ $APP_NAME }} - Dashboard" />
</template>
0 likes
5 replies
undeportedmexican's avatar

Well, the .env file lives in the server, and Vue works exclusively in the client side.

For stuff like changing the title, I send it directly from the server on the app.blade.php entry file.

But if you need to send other information, you may need to add middleware or something.

Are you using something like Inertia, or just vue3 + laravel?

Lumethys's avatar

first of all, you should never use any environment variable in your files. If you need to, pass the environment into the config.php file, and call the config, dont call .env file directly.

as for have to use config value in frontend, there are some package to handle that, but it is dependent on your stack

CamKem's avatar
Level 10

How would I go about passing them through the middleware? Something like this?

'app_name' = {{APP_NAME}};

in the middleware?

martinbean's avatar
Level 80

@camkem You absolutely don’t want to be accessing environment variables from JavaScript. Your environment variables contain sensitive information like database credentials, API keys and secrets, etc. If you’re pulling them into JavaScript then any one will be able to read them!

If you need to access some configuration values then you should be really careful about which ones you actually expose to your JavaScript: https://laravel.com/docs/9.x/vite#environment-variables

1 like

Please or to participate in this conversation.