malico's avatar

Passing --env variables to mix.

Due to the fact that we have very large assets, we decided to split compilation scripts to look like what i have below.

	...,
    "scripts": {
        "dev": "npm run development",
        "development": "mix -- --env section=client",
        "watch": "mix watch -- --env section=client",
        "prod": "npm run production",
        "production": "mix --production -- --env section=client",
        "staff-dev": "npm run staff-development",
        "staff-development": "mix -- --env section=staff ",
        "staff-watch": "mix watch -- --env section=staff",
        "staff-prod": "npm run staff-production",
        "staff-production": "mix --production -- --env section=staff"
    },

In version mix 5.*, we had no problem accessing the section env within the mix file.

if (process.env.section) {
    require(`${__dirname}/js-config/${process.env.section}.mix.js`);
}

process.env.section returns undefined in mix 6.*

any ideas?

0 likes
1 reply
chaudigv's avatar

@malico You may inject environment variables into your webpack.mix.js script by prefixing one of the environment variables in your .env file with MIX_

MIX_SENTRY_DSN_PUBLIC=http://example.com

After the variable has been defined in your .env file, you may access it via the process.env object. However, you will need to restart the task if the environment variable's value changes while the task is running:

process.env.MIX_SENTRY_DSN_PUBLIC

https://laravel.com/docs/8.x/mix#environment-variables

Please or to participate in this conversation.