How to access Laravel env variables in Vue.js | Laravel MIX

by Dillon -

Using Laravel and Vue together is a common approach as it allows the MVC framework to handle the logic, and an application-like user interface for the frontend. Learn how to use Laravel environment variables in Vue.js.

MIX Environment Varaibles

Vue js files are compiled by Laravel Mix. To have Mix compile your environment variables from your .env file you must prefix your variable names with mix_.

A good example of this would be setting the API URL for your SPA application, like so:

MIX_API_URL=http://app.test/api

Laravel Environment Variables in Vue JS

Use Laravel environment variables in Vue JS

To use the variables in your Vue JS application, use the following:

process.env.MIX_API_URL

Laravel .env variables are undefined in Vue js? 

If your environment variables are returning undefined, it is likely that you haven’t compiled the assets since you changed the value of the variable. Sometimes it is easy to leave the watcher running. Cancel the current watch command and re-run it.

Laravel Mix Vue JS Watch for changes

Learn more tips for Vue and Laravel!

Dillon Smart

šŸ‘‹ Hey, I'm Dillon!

Iā€™m a Full-Stack developer in the UK, building websites and applications for around 12 years, landing my first full-time job as a developer in 2015.

Feel free to reach out on Twitter!

Vue 3 Popup Modal Component – Create a Modal - IKnowThatNow

[…] Do you use Vue with your Laravel Application? Learn how to use environment variables in Vue. […]

Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.