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

marklm's avatar

Environment variables in Laravel Mix undefined

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?

0 likes
2 replies
marklm's avatar
marklm
OP
Best Answer
Level 1

Abandoned this to work on other things, and finally came back to it. Found the answer here: https://github.com/JeffreyWay/laravel-mix/issues/1155

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.

1 like
impbob36's avatar

I think this was added in Laravel Mix 2.0 so make sure your on the latest version

Please or to participate in this conversation.