Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

True ENV Values in Laravel Mix

So in Laravel Mix it says in the docs we can add stuff to our .env file prefixed with MIX_ and we can then access it in our JS file when compiled.

I think I may be missing something here though because this doesn't really offer anything along the lines of an env file as the assets are compiled into production mode locally before you push them up to your server. That means the npm run watch and npm run build will both pick up the same env values right?

It works as a kind of global variable thing but not as an environment variable as you can't set the value depending on what the environment is,

This seems kind of obvious so I assume I missing something ??

like image 304
Chris Bell Avatar asked Jan 31 '18 21:01

Chris Bell


People also ask

Where can I find the Laravel Mix package?

Within a fresh installation of Laravel, you'll find a package.json file in the root of your directory structure. The default package.json file already includes everything you need to get started using Laravel Mix.

Why should I use Laravel Mix?

In other words, Mix makes it a cinch to compile and minify your application's CSS and JavaScript files. Through simple method chaining, you can fluently define your asset pipeline. For example: If you've ever been confused and overwhelmed about getting started with webpack and asset compilation, you will love Laravel Mix.

What is Webpack in Laravel Mix?

Because Laravel Mix is built on top of webpack, it's important to understand a few webpack concepts. For CSS compilation, webpack will rewrite and optimize any url () calls within your stylesheets. While this might initially sound strange, it's an incredibly powerful piece of functionality.

How do I add environment variables to a mix file?

You may inject environment variables into your webpack.mix.js script by prefixing one of the environment variables in your .env file with MIX_: After the variable has been defined in your .env file, you may access it via the process.env object.


2 Answers

@Ohgodwhy's answer works, but need slight modification for new mix version

require('dotenv').config()
let webpack = require('webpack')

let dotenvplugin = new webpack.DefinePlugin({
    'process.env': {
        APP_NAME: JSON.stringify(process.env.APP_NAME || 'Default app name'),
        NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'development')
    }
})

mix.webpackConfig({
    ...
    plugins: [
        dotenvplugin,
    ]
like image 102
ken Avatar answered Oct 16 '22 10:10

ken


You can do it like this:

Setup steps:

  1. npm install dotenv --save
  2. Add require('dotenv').config() at the top of your webpack.mix.js file
  3. add let webpack = require('webpack') after #2

Now you can inject these into your build by using the DefinePlugin in your mix declaration:

mix.webpackConfig({
  //...
  new webpack.DefinePlugin({
    'process.env': {
      APP_NAME: JSON.stringify(process.env.APP_NAME || 'Default app name'),
      NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'development')
    }
  })
})

Now your bundled files can use process.env.APP_NAME, for instance, in the application. This safe guards you from exposing your .env file to the browser but allows you to easily share global, insecure values through the full stack.

Notes

Note that the process.env does not get replaced with the .env from Laravel, but rather it exposes it through a merge. Therefore if you are passing, for instance, arguments to npm/yarn run dev (such as NODE_ENV=development), then you do not need to declare NODE_ENV in your .env file. If you do, the .env file will take presedence.

like image 8
Ohgodwhy Avatar answered Oct 16 '22 10:10

Ohgodwhy