14. Node.js Lessons. Script Debugging pt 1.


Our next lesson is devoted to Node.JS debugging. First of all, we will explore the simplest built-in debugger that is called by the node debug command. It looks just like that. Imagine, we’ve got the script:

For example, we would like to stop server.js that is constantly malfunctioning, at a certain point to see its variables and what’s going on. That’s why we put debugger right here:

And launch your script in a debugging mode: node debug server.js. You will need no modules for that purpose. The script initially is paused. As you can see, nothing happens. The bugging mode includes some commands, and right now we need one of them – cont. It will make the script continue its work. As we see, it did continue and even showed log in the end. Good job!

Let us move now to our browser and go to url with this port. Once we’ve done this, the request event happens, a handler gets launched, and execution stops right at the debugger command. Now I can move to the repl console – a command execution mode – and, for example, clear up what urlParsed is or just launch something like:


Now look what’s happened with our browser. Great! It’s working.

We should say, the built-in debugger that we’ve just explored is the simplest of the existing things. It is rarely used only in those cases, when more improved debugging ways do not work for some reason or when you don’t have time or will to launch them.


It is far more convenient to debug using the developer tool in Chrome. You will need the utility named Node- inspector:

npm install -g node-inspector

Install it globally. That’s all for our preparations. And now just a few words on what will happen next. Node.js has a special launching parameter – debug, and you can write a script right after it (–debug server.js). Whenever Node.js is launched with this parameter, it does not only launch server.js, it also starts to listen what’s going on in this port. Another program can connect to the port and give commands to Node.js concerning debugging – for example, to pause or continue the work, get a current value of some variable, etc. These commands are given in full compliance with a special protocol described in the supporting materials for v8. For example, the command


means continue. While this one:


tells Node.js to calculate  1+2. Of course, we won’t write these commands manually, though we could do it. Instead, we use the Node-inspector utility that will send these commands by showing nice web interface to us.

So, I leave Node.js launched and open a new console window, where I will launch the node-inspector. It is a web server you can connect to under the url, which is given by the server, and work with a debugger. It means, I will send commands to the Node-inspector web server, and it will translate them for Node, which listens to the debugging protocol using the v8 debugging language.

So, let’s go to Node-inspector. The design reminds us of the built-in Chrome developing tools, but they are not those ones. Indeed, they look similar to each other because html styles were borrowed from the webkit engine. So, the interface looks quite alike. In fact, it’s just a web page the Node-inspector gives us.

Using a separate window go to:

The following has happened. Once I’d followed the url, a request handler function was launched. If Node.js had been launched without debugging flags, the debugger command would have been ignored, but it has worked in our case. v8 temporarily stopped JavaScript execution and sent this information to a Node-inspector, which had been connected to Node via the port: 5858. Node-inspector received it and, using a web-soсkеt protocol, which we will explore further, sent it to the browser – to browser JavaScript. Here the web interface reacted this action and gave me a pause. The same interface requested data on the stop, which I can use. At the same time, Node-inspector is responsible for translating my actions into commands for the v8 debugger. I can even use the console. If I want to respond the request, I can do the same thing that I’ve done to a built-in debugger – to call a method in a current window.

Everything is quite handy. You are recommended to use it. The only thing you should take into consideration is that we are working with a connection chain. Entering this url to the Node-inspector we connect from client browser JavaScript to server Node-inspector, the latter connects to Node.js and sometimes one of these connections gets broken. Respectively, debugging doesn’t work. In this case you can go to the debugging page again and if it hasn’t been effective, restart your Node-inspector. If this measure does not help you, too, go ahead and re-launch the whole chain: Node, Node-inspector and open the browser page once again. It will definitely work in the end.


The materials were borrowed from the following screencast.

We are looking forward to meeting you on our website soshace.com

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles


How To Use Prospector For Python Static Code Analysis

Prospector is a tool that analyzes Python code, outputs information about the errors, potential problems, convention violation, and complexity of the ...


Fluent Validation in ASP.NET MVC

Fluent validation is a validation library for .NET. This will be separated from the business logic. So, it has an advantage over data annotation ...

How to implement WPF Canvas? Explain with an example

Canvas is a lightweight layout of WPF. Canvas is used for 2D graphic design elements but not for UI. You cannot use it for making text-box, checkbox, ...

No comments yet

Sign in

Forgot password?

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy