How to pass env parameters to a Laravel Mix 6?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Pass Environment Parameters to Laravel Mix 6: A Deep Dive

When developing front-end assets within a Laravel application, managing configuration and build parameters is crucial for creating flexible and reproducible workflows. One common requirement is passing dynamic environment variables from the command line into your build process, specifically into webpack.mix.js. As many developers encounter, trying to pass arguments directly this way often leads to confusion regarding how Node.js processes environment variables.

This post will diagnose why simple attempts fail and provide the robust, industry-standard method for injecting custom parameters into Laravel Mix builds using environment variables.

The Challenge: Why Simple Passing Fails

You are attempting to use command-line flags to inject data directly into webpack.mix.js via process.env. While this seems intuitive, standard shell commands and how Node.js inherits environment variables don't automatically map arbitrary command-line arguments (like --env foo=frontend) into the process.env object within a script execution context like Mix.

When you run mix, it executes its internal logic. If you try to pass custom flags, they often remain as separate arguments rather than being parsed directly into the standard environment scope unless explicitly handled by a wrapper utility. This is why your attempt likely resulted in undefined for variables intended to be loaded via process.env.

The Solution: Leveraging cross-env

The most reliable and cross-platform way to manage environment variables when executing Node-based tools like Laravel Mix is by using the cross-env package. This package ensures that environment variables are set consistently across different operating systems (Windows, macOS, Linux), making your build scripts portable.

Step 1: Installation

First, ensure you have cross-env installed as a development dependency in your project:

npm install --save-dev cross-env

Step 2: Modifying package.json Scripts

Instead of trying to pass the variable directly through complex command structures, we will use cross-env within our package.json scripts to set the necessary environment variables before invoking Laravel Mix.

Modify your package.json file to define a script that explicitly sets the desired environment variables:

{
    "private": true,
    "scripts": {
        "development": "mix",
        "frontend-dev": "cross-env ENV=foo:frontend mix"
    },
    "devDependencies": {
        "laravel-mix": "^6.0.6",
        "cross-env": "^7.0.0"
    }
}

In this example, the frontend-dev script now uses cross-env ENV=foo:frontend to set an environment variable named ENV with the value foo:frontend. While we are setting a custom variable here for demonstration, the principle is that any variables you need must be explicitly defined before execution.

Step 3: Accessing Variables in webpack.mix.js

Now, inside your webpack.mix.js, you can access these variables using the standard Node.js process.env object. Note that we access the environment variable exactly as it was set in the shell:

const mix = require('laravel-mix');

// Accessing the custom environment variable set via cross-env
const customEnv = process.env.ENV; 

console.log(`The custom environment setting is: ${customEnv}`); 

if (customEnv === 'foo:frontend') {
    console.log('Running in development mode for frontend.');
} else {
    console.log('Running in default mode.');
}

// Example usage: conditionally change mix settings based on the environment
if (process.env.ENV === 'production') {
    mix.postCss('postcss.config.js', 'postcss-preset-env', { stage: 2 });
} else {
    mix.postCss('postcss.config.js', 'postcss-preset-env', { stage: 1 });
}

// Your standard mix commands follow...
mix.js('resources/js/app.js', 'public/js');

Conclusion

Passing dynamic configuration to Laravel Mix requires moving beyond simple command-line flags and adopting robust environment management practices. By utilizing tools like cross-env, you ensure that your build scripts are portable and that any custom parameters you define in your shell environment are correctly inherited by the Node process running Webpack Mix. For building scalable applications on the Laravel platform, mastering these environmental details is key to maintaining efficiency and consistency, much like adhering to best practices outlined by the Laravel Company.