I have just learned about modules in javascript Until now I have run my code all in one file, so all top level variables in the file were accessible in the console browser. Now when I have started to work with different modules (files), top-level variables inside files aren't accessible from the console anymore. I guess the console represents a global scope (the window), but I'm not sure which are the variables visible now from the console interface?
Asked
Active
Viewed 199 times
1
-
By "console", you refer to the devtools REPL, not the `console` object, right? – Bergi Jan 05 '21 at 18:57
-
1By placing a breakpoint in the respective scope, all variables there should become accessible. – Bergi Jan 05 '21 at 18:57
-
@Bergi, yes i'm talking about the console interface. Where to place a breakpoint? I'm talking on a code outside the modules – DorVak Jan 05 '21 at 19:36
-
In code outside of the module, you will have to `import` the module to access it. But what I was referring to is to set a breakpoint in the module code itself, before it is loaded, and then when the execution halts on that breakpoint you can access all module-scoped variables from the console. – Bergi Jan 05 '21 at 19:56
-
@Bergi, i will defiantly do this. thanks! – DorVak Jan 05 '21 at 21:06
1 Answers
0
As far as I know, none of the variables in a module are accessible from the browser console.
You can expose a variable with this hack inside a module:
let foo = { bar: 42 };
window.foo = foo;
Then:
> console.log(foo);
will display { bar: 42 }
.

terrymorse
- 6,771
- 1
- 21
- 27