One of the goals of modules is to protect variables and functions defined inside from being accessed outside the script. However, if you want to export a specific function for global use, there are ways to accomplish this. Merely assigning it to the window object may not always be successful:
In your index.html
, consider the following approach:
<script type="module">
const add = (a, b) => a + b;
window.add = add;
</script>
<script>
console.log(window.add(1, 2));
</script>
The result might lead to a
Uncaught TypeError: window.add is not a function
error, which indicates that further adjustments or alternative solutions could be necessary.