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

andrecuellar's avatar

Problem using api key stored on .env using .js file

Hello, I have a google API key on my .env file

MIX_GOOGLE_GEOCODER_API_KEY=MYAPIKEY

I have added on webpack.mix.js the .js file

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

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel applications. By default, we are compiling the CSS
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.js('resources/js/app.js', 'public/js')
    .postCss('resources/css/app.css', 'public/css', [
        require('postcss-import'),
        require('tailwindcss'),
    ]);

if (mix.inProduction()) {
    mix.version();
}

mix.minify([
    'public/js/maps/franchise-map.js',
    'public/css/maps/franchise-map.css'
])
    .version();

In the franchise-map.js file I have added this

let googleGeoCoderProvider = L.Control.Geocoder.google({
    apiKey: process.env.MIX_GOOGLE_GEOCODER_API_KEY,
});

then, I ran the command npm run production and the command artisan optimize for clear all caches, but I have this error on my console browser

Uncaught ReferenceError: process is not defined
    at franchise-map.min.js?id=988e151f438d0d767bff:13:13

I'm using laravel 8

where is the problem?

0 likes
3 replies
aschmelyun's avatar

@andrecuellar I don't think that the mix.minify method lets you use process.env values. You can try to first pass the script through js() and then it might work as intended:

mix.js('resources/js/maps/franchise-map.js', 'public/js/maps')
1 like
aschmelyun's avatar

@andrecuellar It should be automatically minified whenever you run npm run production, but if it doesn't, you can just run the output file through minify like you did above.

Please or to participate in this conversation.