This question already has an answer here:
Answer
Solution:
There are two things to your problem.
The first one is that besides defining the entry in webpack.config
, you have to explicitly import your javascript in your html.
You can do that with the twig helper function encore_entry_script_tags
as described in the page specific scripts section of the manual.
So you would add:
{{ encore_entry_script_tags('photo_page') }}
<script>
// Here goes your script
</script>
Or override your block, if you have one, and call {{ parent() }}
to include any additional scripts you require in all pages.
Note that the entry
tag will add the defer
attribute by default, as configured in config/packages/webpack_encore.yaml
, so you'll need to take that into account and wrap your script to fire on the load
event.
The second one is that webpack doesn't expose modules to the global namespace. For that you would have to do it explicitly in your module:
// Instead of exporting the module, you export it to the global scope
// (the browser window in this case)
global.TaggedImage = TaggedImage;
Source