This article is the 10th part of the tutorial series called Node Hero - in these chapters, you can learn how to get started with Node.js and deliver software products using it.
In this tutorial, you are going to learn debugging your Node.js applications using the debug module, the built-in Node debugger and Chrome's developer tools.
Upcoming and past chapters:
- Getting started with Node.js
- Using NPM
- Understanding async programming
- Your first Node.js server
- Node.js database tutorial
- Node.js request module tutorial
- Node.js project structure tutorial
- Node.js authentication using Passport.js
- Node.js unit testing tutorial
- Debugging Node.js applications [you are reading it now]
- Node.js Security Tutorial
- How to Deploy Node.js Applications
- Monitoring Node.js Applications
Bugs, debugging
The term bug and debugging have been a part of engineering jargon for many decades. One of the first written mentions of bugs is as follows:
It has been just so in all of my inventions. The first step is an intuition, and comes with a burst, then difficulties arise — this thing gives out and [it is] then that "Bugs" — as such little faults and difficulties are called — show themselves and months of intense watching, study and labor are requisite before commercial success or failure is certainly reached.Thomas Edison
Debugging Node.js Applications
One of the most frequently used approach to find issues in Node.js applications is the heavy usage of
console.log
for debugging.
Let's take a look at them!
The debug
module
Some of the most popular modules that you can
require
into your project come with the debug
module. With this module, you can enable third-party modules to log to the standard output, stdout
. To check whether a module is using it, take a look at the package.json
file's dependency section.
To use the
debug
module, you have to set the DEBUG
environment variable when starting your applications. You can also use the *
character to wildcard names. The following line will print all the express
related logs to the standard output.DEBUG=express* node app.js
The output will look like this:
The Built-in Node.js Debugger
Node.js includes a full-featured out-of-process debugging utility accessible via a simple TCP-based protocol and built-in debugging client.
To start the built-in debugger you have to start your application this way:
node debug app.js
Once you have done that, you will see something like this:
Basic Usage of the Node Debugger
To navigate this interface, you can use the following commands:
c
=> continue with code executionn
=> execute this line and go to next lines
=> step into this functiono
=> finish function execution and step outrepl
=> allows code to be evaluated remotely
You can add breakpoints to your applications by inserting the
debugger
statement into your codebase.function add (a, b) {
debugger
return a + b
}
var res = add('apple', 4)
Watchers
It is possible to watch expression and variable values during debugging. On every breakpoint, each expression from the watchers list will be evaluated in the current context and displayed immediately before the breakpoint's source code listing.
To start using watchers, you have to define them for the expressions you want to watch. To do so, you have to do it this way:
watch('expression')
To get a list of active watchers type
watchers
, to unwatch an expression use unwatch('expression')
.Pro tip: you can switch running Node.js processes into debug mode by sending theSIGUSR1
command to them. After that you can connect the debugger withnode debug -p <pid>
.
To understand the full capabilities of the built-in debugger, check out the official API docs: https://nodejs.org/api/debugger.html.
The Chrome Debugger
When you start debugging complex applications, something visual can help. Wouldn’t be great to use the familiar UI of the Chrome DevTools for debugging Node.js applications as well?
Good news, the Chrome debug protocol is already ported into a Node.js module and can be used to debug Node.js applications.
To start using it, you have to install
node-inspector
first:npm install -g node-inspector
Once you installed it, you can start debugging your applications by starting them this way:
node-debug index.js --debug-brk
(the
--debug-brk
pauses the execution on the first line)
It will open up the Chrome Developer tools and you can start to debug your Node.js applications with it.
Great Article
ReplyDeleteNode.js Project Topics for Computer Science
FInal Year Project Centers in Chennai
JavaScript Training in Chennai
JavaScript Training in Chennai
ReplyDeleteEnjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck for the upcoming articles nodejs certification