0

I have a simple html page, which references a javascript module:

<head>
    <script type="module" src="mymodule.js"></script>
</head>

<body onload="doStuff()">
    . . . 

The javascript file looks like this:

export function doStuff() {
    console.log('test');

However, when I run this, I get the error:

columns.html:8 Uncaught TypeError: doStuff is not a function
    at onload (myPage.html:8)

If I change the HTML to:

<script src="mymodule.js"></script>

Then it works; however, this then doesn't let me reference a second javascript library from mymodule.js. My question is, why can it not find the function when it's referenced as a module?

3
  • 3
    shouldn't the <script type="module" src="mymodule.js"></script> be at the bottom of the html script opposed to being on the head ? maybe im wrong idk. Commented Aug 6, 2020 at 16:52
  • Why not using DOMContentLoaded event Commented Aug 6, 2020 at 16:53
  • It doesn't seem to matter where I put the <script type="module" src="mymodule.js"></script> Commented Aug 7, 2020 at 7:12

1 Answer 1

0
<script>
  import { doStuff } from 'mymodule.js';
</script>
<body onload="doStuff()">
....
</body>
Sign up to request clarification or add additional context in comments.

1 Comment

I'm afraid this doesn't make any difference