The Laravel documentation on Compiling Assets says that you can use environment variables in your .env file in mix by prefixing them with MIX_, and access them with process.env.MIX_..., but this isn't working for me. When I try to access environment variables in webpack.mix.js, they come back undefined.
All I'm doing is defining MIX_VAR in .env, and in webpack.mix.js, doing: console.log(process.env.MIX_VAR); When I run npm run dev, it logs 'undefined'.
Am I not understanding this, or is there something else I need to do to get this to work?
In order to use your dotenv variables in webpack.mix.js, you have to add this line at the top of your webpack.mix.js:
require('dotenv').config();
Also, if you do this, you don't need to prefix your .env variables with MIX_ like it says in the documentation, you can now access any .env variable in Laravel Mix.