How to Debug nodeJS app and enable log points using VS editor.

Posted By :Anubhav Garg |26th July 2019

 

Debugging - the process of tracing project workflow and removing bugs or adding new logics in application - can be a challenging task to do in node.js or in javascript.

 

Its a tricky or typical task but we have multiple methods to debug by using the visual code editor.

 

Options to Debug in Node.js

There’s a number of ways to debug your node.js program, I’ve listed them out below.

 

Console.log() —  It’s built into Node.js and prints in the terminal just like it’s built into JavaScript and prints in the browser’s console or in terminal.

 

Using the console on every return parameters of functions is not a good coding approach, the reason is that if you leave any console in the production, other developers will open the console and see the response and use it for own purpose.

 

--inspect — The Node.js docs themselves understand debugging isn’t the easiest, so they’ve made a method to help developers for debugging.

 

Firstly setup the vs code editor and change the setting to enable debugger.

Go to Preferences > Settings and in the search box type in “node debug” & click the first box: Debug > Node: Auto Attach and set the drop-down to “on”.

 

 

Now, open your Node.js project file, and set some breakpoints by clicking on the left-hand side represented as red points of the file wherever you’d like to see your code stop.

 

 

Now open the terminal and type nodemon --inspect server/server.js & when the function is hit in the browser, it stops the code and screen will navigate to the breakpoints.

 

LogPoints

 

A Log point is a variant of a breakpoint that does not "break" into the debugger but instead logs a message to the console & represented by a "diamond" shaped icon. Log messages are print in terminal and parameters are pass in curly braces ('{}').

 

To enable log points  follow these steps:

 

 

Select the Edit Breakpoints > Log message

 

 

Enter your log message and the see terminal for the result.

 

 

Conclusion

Node.js debugging is not the hard or time taking task, and it doesn’t need to involve 500 console.log()s in the app to figure out the workflow.

Visual Studio Code’s EditorDebug > Node: Auto Attach setting makes that a thing very easy.

 

 

 


About Author

Anubhav Garg

He is an Intellective Web Application Developer. He is good in NodeJs, Angular & MongoDb with knowledge in AWS. He is always motivated to learn new things.

Request For Proposal

Sending message..

Ready to innovate ? Let's get in touch


Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Chat With Us