# Why can't I add a JS file executed as a template to a JS bundle?

**URL:** <https://discourse.gohugo.io/t/why-cant-i-add-a-js-file-executed-as-a-template-to-a-js-bundle/41056>\
**Category:** support\
**Created:** [October 22, 2022, 5:54pm UTC](https://discourse.gohugo.io/t/why-cant-i-add-a-js-file-executed-as-a-template-to-a-js-bundle/41056 "2022-10-22T17:54:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![realpixelcode](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/realpixelcode/32/17189_2.png) [@realpixelcode](https://discourse.gohugo.io/u/realpixelcode)\
**Post date:** [October 22, 2022, 5:54pm UTC](https://discourse.gohugo.io/t/why-cant-i-add-a-js-file-executed-as-a-template-to-a-js-bundle/41056/1 "2022-10-22T17:54:48Z")

</div>

I’ve got a JS file called `random.js` which is supposed to open a random page of my website when the user clicks a `Random article` button. Obviously, the JS file needs to know about the pages that exist, so I made Hugo print the URLs into a JS array, using the `resources.ExecuteAsTemplate` function.

This works fine when adding the rendered JS file as a stand-alone script to the HTML.

However, when trying to bundle the `random.js` file with other JS files into `bundle.js`, it isn’t included.

This works fine (but it’s a separate file):

```auto
{{ $jquery := resources.Get "js/jquery.js" }}
{{ $menuJS := resources.Get "js/menu.js" }}
{{ $randomJS := resources.Get "js/random.js" | resources.ExecuteAsTemplate "js/random-ready.js" . }}

{{ $minifiedJS := slice $jquery $menuJS | resources.Concat "js/bundle.js" | resources.Minify }}

<script src="{{ $minifiedJS.Permalink }}" defer></script>
<script src="{{ $randomJS.Permalink }}" defer></script>

```

Here, `random-ready.js` isn’t included in `bundle.js`:

```auto
{{ $jquery := resources.Get "js/jquery.js" }}
{{ $menuJS := resources.Get "js/menu.js" }}
{{ $randomJS := resources.Get "js/random.js" | resources.ExecuteAsTemplate "js/random-ready.js" . }}

{{ $minifiedJS := slice $jquery $menuJS $randomJS | resources.Concat "js/bundle.js" | resources.Minify }}

<script src="{{ $minifiedJS.Permalink }}" defer></script>

```

Why doesn’t this work? How to do it correctly?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [October 23, 2022, 6:30pm UTC](https://discourse.gohugo.io/t/why-cant-i-add-a-js-file-executed-as-a-template-to-a-js-bundle/41056/2 "2022-10-23T18:30:31Z")

</div>

Which version of Hugo are you using?  
Does this happen when running `hugo`?  
Does this happen when running `hugo server`?  
Is this an intermittent problem?

---

<div class="post-metadata">

**Author:** ![realpixelcode](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/realpixelcode/32/17189_2.png) [@realpixelcode](https://discourse.gohugo.io/u/realpixelcode)\
**Post date:** [October 23, 2022, 8:58pm UTC](https://discourse.gohugo.io/t/why-cant-i-add-a-js-file-executed-as-a-template-to-a-js-bundle/41056/3 "2022-10-23T20:58:47Z")

</div>

> Which version of Hugo are you using?

Thanks for the hint, that’s it. I haven’t checked my GitHub notifications for a while; after installing v0.104.3, the issue was gone. 👍

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [October 25, 2022, 8:59pm UTC](https://discourse.gohugo.io/t/why-cant-i-add-a-js-file-executed-as-a-template-to-a-js-bundle/41056/4 "2022-10-25T20:59:43Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
