DEV Community

Cover image for Debugging nodejs easy, simple, without complications.
Diego Uribe Gamez
Diego Uribe Gamez

Posted on • Updated on

Debugging nodejs easy, simple, without complications.

The professional way to debugg an application that is running in Nodejs on the server side is as follows:

  1. First, we add the flag inspect in the command that runs our server js.
$ node --inpect file.js
Enter fullscreen mode Exit fullscreen mode
  1. second, in our Chrome or Chromium browser we open the url chrome://inspect/#devices and configure a new device:
    URL to inspect nodejs services

  2. Third, we configure the device in localhost and port 9229, which is the default port:
    Conff debugger in chrome

And voila, this way we can discuss our service locally:
Debugging nodejs aplication

If we need to adjust the port, or add it to an inspector of a client such as Visual Studio Code, or stop the inspector in the first line to be able to debut before the start we can see the official documentation and these could be the results:

$ node --inpect=0.0.0.0:9229 file.js
$ node --inspect-brk file.js
Enter fullscreen mode Exit fullscreen mode

Node.js official Debugging Guide

Beyond the professional aspect

There are two problems that we face when working in this way, the first is that if we want to make a change we must stop and restart our server, the second is that if the application dies we must restart our server again.

To solve this problem we will use two programs:

  • Forever: your job will be to maintain and restart our nodejs server in case our application dies.
  • Nodemon: it will be in charge of monitoring our application files in case we make any changes to it.

we install forever and nodemon

$ npm install -g nodemon@1.19.4
$ npm install -g forever@1.0.0
Enter fullscreen mode Exit fullscreen mode

our server will run as follows

$ forever -c 'nodemon --watch /opt/app --inspect=0.0.0.0:9229' file.js
Enter fullscreen mode Exit fullscreen mode

With this we can develop faster and we will not waste time when working our service.

I hope you liked the content, if you have any questions or want to write about a related topic, please leave your comment, until next time.

Top comments (0)