Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug JavaScript modules using the browser console?

I am using my JavaScript file as a module:

<script type="module" src="main.js"></script>

but, when my HTML is loaded, any object inside the main.js is not accessible from the browser debugging console, saying that the variable is not defined.

main.js:

var MyVar = 10;

enter image description here

When putting a breakpoint in main.js I can access the objects fine. But then the program is stopped, and I want to manipulate some while everything is running.

How can I access (global) objects in the JavaScript files I have included as module?

like image 652
Bart Friederichs Avatar asked May 14 '26 21:05

Bart Friederichs


1 Answers

You can put the breakpoint in the module, then when the program is stopped, inspect the scope to find the object that you are looking for, then right-click and store as global variable. Even after continuing execution, the object will be available in the global scope under that variable. You can also do this manually by putting an assignment window.myGlobal = interestingObject either directly in the module code or enter it in the console while stopped at a breakpoint.

like image 179
Bergi Avatar answered May 16 '26 12:05

Bergi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!