True ENV Values in Laravel Mix

后端 未结 3 1416
面向向阳花
面向向阳花 2021-02-15 18:33

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 missi

3条回答
  •  悲哀的现实
    2021-02-15 18:53

    @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,
        ]
    

提交回复
热议问题