Search code examples
laravelvue.jsenvironment-variableslaravel-mix

Add Laravel .env variable to Vue component


I would like to get access to an .env variable using Vue JS.

In my .env file I have added the 'MIX_' prefix to the var.

MIX_VAR=key

And then in the vue component, I have in the created():

console.log(process.env.MIX_VAR);

I keep getting undefined as the result.

I have tried clearing config cache, but still getting the same issue. Any ideas?


Solution

  • You must build your JS for the env variables to be replaced. You can do this with npm or yarn

    https://laravel.com/docs/5.7/mix#running-mix