I have a script tag inside my HTML with type module, and it exports a variable:
<script type="module">
var life = 42;
export { life };
</script>
How do I access life
from other script tags or the Chrome console for debugging?
This seems to be highly ranked in SEO and the real answer from @Aplet123 is a little buried in the comments.
You cannot access variables across/outside of modules, this is the whole point. You can export/import but this won't work in the browser console.
You can attach any object to window in order to expose it globally. So adding window.life = life
will expose life
(or window.life
) in the browser console.
This is a solid option when you're playing around or debugging issues.
This shouldn't be used in production except under limited circumstances – for example if you need to load JavaScript without access to the network (including through a ServiceWorker) or you want the code to be in one file to be easily inspected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With