Node.js Inspector
What is inspector in Node.js?
Inspector in node.js is a debugging interface for node.js application that is contained in the app.js file and used blink developer tools. It works almost similar to chrome developer tools. It can support almost all the feature that a debugger generally have such as navigating to source files, setting breakpoints, CPU and heap profiling, network client requests inspection, console output inspections, and many other features.
How to install?
It can be installed by running the following command in the command line after installing npm (node package manager).
$ npm install -g node-inspector
Here in the command, -g flag corresponds to global installation of the inspector. After installing if you run command node-inspector, we get an output like this:
In the above figure, it displays an URL for debugging purpose. So, when we point our browser to http://127.0.0.1:8080/?port=5858, we get a GUI for debugging. Sometimes, port 8080 may not be available on the computer then we will get an error. We can change the port (in this case port 5555) on which node-inspector is running by using the following command:
$ node-inspector --web-port=5555
How to start using it?
It can be started using following command in command line:
$ node-debug app.js
where app.js is the name of the main JavaScript application file. Available configuration options can be seen here.
The node-debug command will load Node Inspector in the default browser.
Note: Node Inspector works in Chrome and Opera only.
Advanced Use: While running node-debug is an easy way to start your debugging session, sometimes we need to tweak the default setup. Then we need to follow three steps given below
-
Start the node-inspector server: This can be done by running command:
$ node-inspector
The server can be left running in the background, it is possible to debug multiple processes using the same server instance.
-
Enable debug mode in the node process: You can either start Node with a debug flag like:
$ node --debug your/node/program.js
or, to pause your script on the first line:
$ node --debug-brk your/short/node/script.js
Or one can enable debugging on a node that is already running by sending it a signal:
- Get the PID of the node process using your favorite method. pgrep or ps -ef are good.
$ pgrep -l node
2345 node your/node/server.js
-
Send it the USR1 signal
$ kill -s USR1 2345
- Get the PID of the node process using your favorite method. pgrep or ps -ef are good.
- Load the debugger UI: Open http://127.0.0.1:8080/?port=5858 or the produced URL in the Chrome browser.
For more details we can refer to the official documentation.
Contact Us