ReferenceError: exported function is not defined at onload

Viewed 18
<html>
<head>
<script type="module" src="topo_space.js"></script>
</head>
<body onload="main()">
</body>
</html>

topo_space.js:

export function main(){
    const innerHTML = `<svg id="main_svg" width="959" height="704"></svg>`;
    console.log(innerHTML);
    document.body.innerHtml = innerHTML;
}

Getting error:

topo_space.html:6 Uncaught ReferenceError: main is not defined at onload

I want to stick to using module, because the js can itself further import a json as module, which I find extremely cool.

1 Answers

Checkout this thread to read more information about this problem:

How to use code from script with type=module

The answer is:

window.main = function main() {
  const innerHTML = `<svg id="main_svg" width="959" height="704"></svg>`;
  console.log(innerHTML);
  document.body.innerHtml = innerHTML;
};
<html>
  <head>
    <script type="module" src="topo_space.js"></script>
  </head>
  <body onload="main()"></body>
</html>

Related