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

By Dillon Smart · · · 1 Comments

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!

LaravelVue

1 Comment

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. […]

Was this helpful? Leave a comment!

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

How to use Laravel Factory in Database Seeders

Updated 16th August 2022

Laravel factories are a great way to add test data to your database in bulk. This saves time, removing the need to write methods to insert data for each column manually. Follow along to learn how to use Laravel Factory in Database Seeders.  To get started, create a new model. The model we will create

Laravel php artisan make:auth command not defined

Updated 11th August 2022

Laravel 6.0 LTS came with a lot of changes to the framework, apart from moving the the new Semantic Versioning, Laravels famous php artisan make:auth command was dropped. php artisan make:auth Before the Laravel make:auth command would create all the Migrations, Models, Controllers and Routes needed to easily setup an authenticatable application in seconds. However,

Laravel Undefined Constant header_x_forwarded_all after upgrading Laravel version

Updated 1st January 2024

Upgrading to a newer version of Laravel can be exciting, but it can also be nerve racking if you are unfamiliar with the changes in newer version, especially if your upgrading multiple times. Have you received an “Undefined Constant” error in Laravel after upgrading? In this post, I will help you understand the changes under