It's likely you could be getting this error because your
body > script
tags are being processed before the entire document has had the chance to load it's required resources, especially since you've added the
defer
attribute (which tells a script not to execute until the page has been completely parsed).
You need to add proper document state processing to ensure that any JS built into the page isn't run until the document is ready:
function onLoaded(fn) {
if (document.readyState == 'complete' || document.readyState == 'interactive')
setTimeout(fn, 1);
else
document.addEventListener('DOMContentLoaded', fn);
}
onLoaded(function () {
out('Hello');
});
Alternatively you could consider putting the JS in another JS file and include it in the page after the bundle.js file. Also, fair credit to
this StackOverflow answer[
^] for the basis of this answer.