How could I see the variables of a local server running Node.js by "Inspect" the browser?

Asked

Viewed 105 times

-1

For a local HTML the global variables can be seen by the browser with Inspect, but I cannot see the variables running locally the same code with Node.js together with Express. I’d like to see the variables to see if they exist and if they’re working properly.

2 answers

4

To debug a Node.js process you must start the process with the option --inspect, that will initialize Inspector which is the debugging server for Node.js .

Example:

node --inspect

The process will start and a message will appear on the console:

Debugger Listening on Ws://127.0.0.1:9229/0f2c936f-b1cd-4ac9-aab3-f63b0f33d55e

This URL is the address where the Inspector will listen to the debugger clients using the protocol Chrome Debugging Protocol View. Inspector clients must know this address, port and host UUID to connect.

PS: This url is random at each Inspector call a different url is generated.

To connect using Chrome Devtools open the url chrome://inspect in a Chromium-based browser or edge://inspectno in Microsoft Edge.

inserir a descrição da imagem aqui

Click the button Configure... and pass the url where the Inspector is listening to if the host is listed just click on Inspect.

Made the connection make a test:

var x = 100;
var y = "Pt Stack Overflow";

inserir a descrição da imagem aqui

Obs: If you want use the debugger in another port other than standard(9229) use syntax:

node --inspect=[porta]

Where [porta] is the port where Node.js will accept Devtools connections.

To learn more:

  • I got to the Chrome Inspect part, but I couldn’t find the server listed, and I couldn’t add it through the Configure button. Is there anything I can do to get around this problem? I’ve already activated Developer Tools experiments from Chrome, and I’ve installed Chrome-remote-interface.

  • I don’t know how these settings affect the debugger and its connectivity, I advise you to use the default settings. As for the url generated by node --inspect they are random the same url generated for me will not be useful for you so you have to use the url generated in your console immediately the call node --inspect.

0


Well, the answer of Augusto Vasques did not solve my problem, which would be to see the variables and functions of the modules of the client-side of Node.js through Inspect Chrome (browser I use).

After a lot of research, I figured out how to see and manipulate variables and functions. In the modules or in the main code (not in the index.js or app.js file of Node.js, commonly called, but in the modules that will be used by the browser, client-side), instead of declaring the variables and functions commonly so:

var <variável> = 100;
let <variável2> = "Frase";
const <variável3> = 3.1415; // Pi 

do:

globalThis.<variável> = "Conteúdo da variável".length; // === 20
globalThis.<nome_da_função> = function ( params ) { <bloco de código> };

OR in place of "globalThis" one can also put "window", which refer to the same object, which is the browser.

NOTE: BUT BE AWARE THAT IT ONLY SERVES TO DEVELOP THE CODE AND TEST IT THROUGH THE BROWSER!!! It shouldn’t be final product because your code is vulnerable! Soon you should turn back to:

var/ let/ const <variável> = <Conteúdo>;

function <nome_da_função> ( params ) { <bloco de código> }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.