1

If I'm using the Node command line, I can look at global variables declared with var by displaying the global object, like so:

> global

The global variables are displayed as properties at the end of the global object.

If I declare a variable with let, then the variable is getting stashed in the "script scope" (I'm assuming; similar to what happens in the browser).

If I declare a top-level variable with let in the browser, I can view these script-scoped variables using the debugger.

Can I somehow look at script-scoped variables in Node?

Thank you.

Elisabeth
  • 2,972
  • 6
  • 35
  • 39
  • Are you asking how to debug a node module and see the private variables there? In general global variables are not recommended, better to expose what you need through a shared module - https://stackabuse.com/using-global-variables-in-node-js/ – doublesharp Apr 01 '19 at 18:19
  • Yes I totally understand global variables are a no no, but I'd like to understand how to view the "script scope" in Node, just for my own educational purposes :-) – Elisabeth Apr 01 '19 at 18:20
  • 1
    I'm not sure what you mean by script scope - the current running process? You can start your script with the `--inspect` flag to start the debugging port and then use Chrome debugging tools to attach to it and inspect variables or put in a break and then you can run arbitrary scripts in the current scope via the console. – doublesharp Apr 01 '19 at 18:30
  • https://nodejs.org/en/docs/guides/debugging-getting-started/ – doublesharp Apr 01 '19 at 18:30
  • Possible duplicate of [View list of all JavaScript variables in Google Chrome Console](https://stackoverflow.com/questions/2934787/view-list-of-all-javascript-variables-in-google-chrome-console) – lifeisfoo Apr 01 '19 at 18:31
  • @lifeisfoo not a duplicate, this is a question about node debugging not a browser. – doublesharp Apr 01 '19 at 18:32
  • Clearly, I wasn't clear in my question :-) Sorry about that. By script scope, I mean the environment where variables declared at the top level with "let" are stored. Before "let", variables were declared with "var", which are stored in the global object. You can "see" this global object in node by typing global at the command prompt. Global variables declared with "let" are stored somewhere, in a context (probably an object) that is available to all code (because they are global). So my question is: how can I see this context object for variables declared with "let"? – Elisabeth Apr 02 '19 at 19:57
  • Could be the answer is "you can't" - that the context object for variables declared with let doesn't have a public facing hook like the global object does. Which is fine; I'm just curious to know :-) – Elisabeth Apr 02 '19 at 19:59

1 Answers1

0

If you want to see what you get from the globals you can do this.

In your code (index.js):

console.log(global)

Then you can save the output in a file from the terminal:

node index.js > global.json

OR

node index.js > global.txt