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

huwcarwyn's avatar

Vapor - ASSET_URL environment variable is not present during build step?

I am trying to make sure that my code-split application knows where to access individual chunks by using the following code in my webpack.config.js

const ASSET_URL = process.env.ASSET_URL + '/'

module.exports = {
...
    output: {
        publicPath: ASSET_URL
    }
...
}

I've left out some code I felt irrelevant to this post but I will add the rest if anyone wants me to.

My problem comes after I run vapor deploy production, I see that a chunk has failed to load in my browser console, and when inspecting the network tab I can see that see that it has tried /undefined/js/0.js - suggesting that ASSET_URL is undefined.

Has anyone else had this problem? Any help would be greatly appreciated.

0 likes
1 reply
huwcarwyn's avatar

Writing this an answer to anyone else that experiences the same problem:

I put this in the blade template that renders my application to set a global ASSET_URL variable from my config value:

    <script charset="utf8" type="text/javascript">
        window.ASSET_URL = "{{ config('app.asset_url') }}"
    </script>

Then in my entry javascript file I put this to tell webpack where to load the chunks from:

if (process.env.NODE_ENV === 'production') {
  __webpack_public_path__ = window.ASSET_URL + '/'
}

Make sure to add this to your webpack's plugins array to make process.env.NODE_ENV available to your app:

    new webpack.DefinePlugin({
      'process.env.NODE_ENV': process.env.NODE_ENV
    })

Please or to participate in this conversation.