Node.js Debugging

07 Oct, 2021
Node.js Debugging: Debugging is a concept to identify and remove errors from software applications. In this article, we will learn about the technique to debug a Node.js application.
Why not to use console.log()?Using console.log to debug the code generally dives into an infinite loop of “stopping the app and adding a console.log, and start the app again” operations. Besides slowing down the development of the app, it also makes the writing dirty and creates unnecessary code. Finally, trying to log out variables alongside with the noise of other potential logging operations, may make the process of debugging difficult when attempting to find the values you are debugging.
How to debug?Mostly we used console.log() but as mentioned above, it is not always a good practice. We can use a V8 inspector for it.
Steps for debugging:
Write the following code in the terminal window as shown below:node --inspect-brk-filename.jsOpen your Chrome browser and write inspect as shown below:Now click on Open Dedicated DevTools for Node.Now, click on the Node.js icon. The terminal will show the following message:
Write the following code in the terminal window as shown below:node --inspect-brk-filename.js
node --inspect-brk-filename.js
Open your Chrome browser and write inspect as shown below:
Now click on Open Dedicated DevTools for Node.
Now, click on the Node.js icon. The terminal will show the following message:
Other tools to help launch a DevTools window:
june07.com/nim
github.com/jaridmargolin/inspect-process
github.com/darcyclarke/rawkit

Additional Debugging APIs:
Debugging an existing Node process:process._debugProcess(pid);</pre


GDB-like CLI Debugger:
node inspect filename.js
Drive with DevTools Protocol via WS port:const dp = require('chrome-remote-interface');  async function test() {const client = await dp();const {Profiler, Runtime} = client;  await Profiler.enable();await Profiler.setSamplingInterval({interval: 500});  await Profiler.start();await Runtime.evaluate({expression: 'startTest();'});await sleep(800);  const data = await Profiler.stop();require('fs').writeFileSync('data.cpuprofile',                JSON.stringify(data.profile));};  test().then((result)=>{    console.log(result);}).catch((error)=>{    console.log(error);});DevTools Protocol via require('inspector'):const inspector = require('inspector');const fs = require('fs');const session = new inspector.Session();  session.connect();session.post('Profiler.enable');session.post('Profiler.start');  setTimeout( function() {    session.post('Profiler.stop',              function(err, data) {        fs.writeFileSync('data.cpuprofile',             JSON.stringify(data.profile));    });}, 8000);
Debugging an existing Node process:process._debugProcess(pid);</pre

process._debugProcess(pid);</pre


GDB-like CLI Debugger:
node inspect filename.js

node inspect filename.js

Drive with DevTools Protocol via WS port:const dp = require('chrome-remote-interface');  async function test() {const client = await dp();const {Profiler, Runtime} = client;  await Profiler.enable();await Profiler.setSamplingInterval({interval: 500});  await Profiler.start();await Runtime.evaluate({expression: 'startTest();'});await sleep(800);  const data = await Profiler.stop();require('fs').writeFileSync('data.cpuprofile',                JSON.stringify(data.profile));};  test().then((result)=>{    console.log(result);}).catch((error)=>{    console.log(error);});
const dp = require('chrome-remote-interface');  async function test() {const client = await dp();const {Profiler, Runtime} = client;  await Profiler.enable();await Profiler.setSamplingInterval({interval: 500});  await Profiler.start();await Runtime.evaluate({expression: 'startTest();'});await sleep(800);  const data = await Profiler.stop();require('fs').writeFileSync('data.cpuprofile',                JSON.stringify(data.profile));};  test().then((result)=>{    console.log(result);}).catch((error)=>{    console.log(error);});
DevTools Protocol via require('inspector'):const inspector = require('inspector');const fs = require('fs');const session = new inspector.Session();  session.connect();session.post('Profiler.enable');session.post('Profiler.start');  setTimeout( function() {    session.post('Profiler.stop',              function(err, data) {        fs.writeFileSync('data.cpuprofile',             JSON.stringify(data.profile));    });}, 8000);
const inspector = require('inspector');const fs = require('fs');const session = new inspector.Session();  session.connect();session.post('Profiler.enable');session.post('Profiler.start');  setTimeout( function() {    session.post('Profiler.stop',              function(err, data) {        fs.writeFileSync('data.cpuprofile',             JSON.stringify(data.profile));    });}, 8000);
Another awesome thing in using Chrome as a debugging tool is that you can debug both your front-end and back-end JavaScript code with the same interface.
Node.js-Basics
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.