Skip to content
Related Articles
Open in App
Not now

Related Articles

How to enable debugging in Express App ?

Improve Article
Save Article
  • Last Updated : 25 Mar, 2022
Improve Article
Save Article

In express, there is a module present called DEBUG that gives log information. It tells about middleware functions, application mode, their status, and also about the requests and responses made to the server.

To use this module while running the express app, set the DEBUG  environment variable to express:* 

$ DEBUG=express:* node index.js

The output generated on running the command is shown below:

These logs help in finding any bug in the program and tell which part of the program is creating the problem. 

Sometimes you may not want the entire log but only the logs from a specific part of the program, for example, only the logs of the router or application, then you can do so by setting the DEBUG environment variable as express:* and then running the command:

$ DEBUG=express:router node index.js

This gives logs only of the router part. Similarly, you can do for application as well.

$ DEBUG=express:application node index.js

If you want for both of them you can do so by running the code:

DEBUG = express:application,express:router node index.js

In the example above, whose image is shown we have set DEBUG to express:* . Here, * indicates that all the areas are included and all the logs will be shown. 

For windows

The above-shown method is for Linux. For Windows, you need to edit the package.json file. In the package.json file set your start command as shown and run npm start.

"scripts": {
   "start": "set DEBUG=express:* & node index.js"    
}

You can also use more than one option at a time to debug them together by separating them by commas. Example:

$ DEBUG=mail,express:* node index.js

Debug environment variables

Suppose you want the logs to be visible in a different manner. Suppose you don’t want the colors to appear or you want to see some hidden information, you can do that by setting environment variables. Some of these variables are:

  1. DEBUG: Select the namespaces for which you want to see the logs.
  2. DEBUG_COLORS: By setting this to either 1 or 0 you can decide whether the logs that will be visible will be in different colors or the same white color. If you set DEBUG_COLORS to 0 the logs will appear in white plain text. By default, it is set to 0.
  3. DEBUG_HIDE_DATE: This can hide the date from debug output.
  4. DEBUG_SHOW_HIDDEN: This shows hidden properties on inspected objects.

Example: You don’t want the log output to be displayed in different colors, set DEBUG_COLORS =1

$ DEBUG=express:* DEBUG_COLORS=0 node gfg1.js

Its output is shown below:

Reference Link: https://expressjs.com/en/guide/debugging.html

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!