How to debug Node JS Errors

Jayant - Aug 3 '22 - - Dev Community

🕸️Node JS

  • It is a server-side platform built using Google Chrome’s JavaScript Engine (V8 Engine)
  • It is an Open Source cross-platform runtime environment for developing server-side and networking applications.
  • Over the Past decades, it become very Popular but It fails at some points

There are 3️⃣ types of error that you can Encounter :

1) 😵Syntax Error

  • This Occurs when we make any typo in the code

    LIKE, Instead of console.log(x) you wrote consle.log(x)

    SOLUTION ⇒

    To Avoid This type of Error you can use some 😄 Good Code Editor Like ,

    • VS Code
    • Atom

    They Support all type of Programming Languages.

    You can also Download Code Linters Like ESLint . It will report syntax Errors , bad Indentations and undeclared Variables.

2) 😕Logic Error

  • It Occurs when something doesn’t work as expected .
  • This can be due to IN-Complete Logic

SOLUTION ⇒

Go through the Logic of your code and find what could be the Problem.

U can use the console.log to find what the problem is ?

3) 🤕Run Time Error

  • It occurs when the application is executed in real-time.
  • often It’s hard to Replicate the same Error Again

It is very hard to spot the Error and sometime it takes very high time to solve the Error.

SOLUTION ⇒

To solve this type of Error we have to use the Debuggers , to Debug the code Line-by-Line.

There are various Debuggers Out there in the Market.

  • Code Editors have their in-built Debugger
  • Using the Chrome Browser to Debug
  • ***Use a Third-Party Logging System [They are More Feature Rich ]*

1) Using Code-editor Debugger

Steps to start Debugging in the VS Code [or any other code Editor.]

  • Open the Index.js File.
  • Open the Run and Debug Panel.
  • Set the Breakpoint.
  • Open the Run and Debug Button.
  • Choose Node.js environment.

After that a Debugger Toolbar appears at the top of the window.

VS Code Debugger

  • step over: Execute the next command but stay within the current function — do not jump into any function it calls
  • step into: Execute the next command and jump into any function it calls
  • step out: Continue processing to the end of the function and return to the calling command
  • restart the application and debugger
  • stop the application and debugger

2) Using Chrome Bowser to Debug

To debug the code in the browser , while starting the server instead of node index.js type node --inspect index.js or nodemon --inspect index.js

You can also use --inspect-brk instead of --inspectto halt processing (set a breakpoint) on the first line so you can step through the code from the start.

Open the chome://inspect

Chrome Devtools

  • Click on Configure and then add the port number , where the site is Running .

After that , Right-click on the browser[make sure you are on the app tab] and click on inspect . Inspect panel will open.

Go to the Source Panel and then open the file by hitting ctrl + P and entering the filename [index.js]

Now you can use it as a debugger , click on Any line to set the Breakpoint , The Debugger will show up .

Now you can Debug Your Code.

3) Using Third-Party Logging System

Third-Party Logging System provides more features to debug and Maintain Your Code.

It is More Feature Rich and fast.

Some of the Third Party App.

ErrSole →

ERRSOLE - Fix Your Node.js Errors under 5 Minutes

Getting Starting Guide

Installation



npm install errsole


Enter fullscreen mode Exit fullscreen mode

Usage



/**
 * Put this Errsole code snippet at the top of your app's main file
 */
const errsole = require('errsole')
errsole.initialize({
  framework: 'express',
  token: '022b2784-cdc2-4072-94ce-6bf067a1809e'
})
// End of Errsole code snippet


Enter fullscreen mode Exit fullscreen mode

Example



/**

  • Put this Errsole code snippet at the top of your app's main file */ const errsole = require('errsole') errsole.initialize({ framework: 'express', token: '022b2784-cdc2-4072-94ce-6bf067a1809e' }) // End of Errsole code snippet

/**

  • Your app code starts here */ const express = require('express') const app = express()

app.get('/', function (req, res) {
res.send('Hello World')
})

app.listen(3000)

Enter fullscreen mode Exit fullscreen mode




Other 3rd Part App

Roll-Bar

New Relic One

Airbrake

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .