Be the first user to complete this post

  • 0
Add to List

Debugging nodejs applications using node-inspector and Chrome Dev Tools

Lets face it, printing console.logs on your app server console is not really an ideal strategy for debugging your nodejs applications. Would'nt it be nice if you could just use the chrome dev tools to debug your serverside nodejs code? Turns out that there is a way to do that. And thats exactly what we will set out to achieve in this article. All the source code for this tutorial is combined with the node starter kit and can be found on github in this particular commit Here's what you would be able to do by the time you reach the end. Create a npm script that lets you run your server in debug mode Open Chrome's dev tools and be able to debug your nodejs applications the same way you debug your client slide javascript applications.

Basic Setup

First install node inspector globally
sudo npm install -g node-inspector
At the moment, if you want to, you can simply run the following command
node-debug app.js
And you will be able to access the url http://localhost:8080/debug?port=5858 and debug your applications. However, we want to take this a little further and convert it into a configurable npm run script. Lets see how we can do that. Advanced setup The aim of this setup is to run your node server in debug mode as a grunt task what whose configuration can be specified in a file instead of doing it on the command line.
grunt debug --file=app.js
As you can see, we created a command line argument called 'file' that takes a file name which, as you shall see later, is passed to the node-debug command. The first thing to do is to install the 'grunt-run' package in your project. The 'run' task lets you run any command that can be run from the terminal.
npm install grunt-run --save-dev
The next thing to do is to configure 'run' with a command that we want to execute. We can do that by specifying a task. Lets call that task 'node-inspector' for consistency. We will keep all of our config for the run command in grunt\run.js and export it as a module
module.exports = {
  'node-inspector': {
    cmd: 'node-debug',
    args: []
  }
}
I have left the args array empty for the moment, but you can use any of the arguments mentioned here. The next step is to configure it as a grunt task. Below you can see my gruntfile. It already contains a jshint task as a default grunt task. All I do is create load the run configuration and prepend the filename that is specified in the user input to the args array.
module.exports = function(grunt) {

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json')
  });

  // Read the config for the 'run' task that can run any command
  var runConfigs = require('./grunt/run.js');

  if(!runConfigs['node-inspector'].args){
    runConfigs['node-inspector'].args = [];
  }

  // Prepend the file name to the command line arguments array 
  // for the node-inspector task
  runConfigs['node-inspector'].args.unshift(grunt.option('file'));

  grunt.config( 'jshint', require('./grunt/jshint.js') );
  grunt.config( 'run', runConfigs );

  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-run');

  grunt.registerTask('default', ['jshint']);
  grunt.registerTask('debug', ['run:node-inspector']);

};
If you are like me and like to keep all of your scripts runnable as npm scripts, add this line to your scripts object in package.json
scripts:{
    // other scripts
    // ...
    "debug": "grunt debug --file=app.js"
}



Also Read:

  1. What is npm shrinkwrap and when is it needed
  2. Setup nginx with multi domain websites running on nodejs
  3. Unit test your Nodejs RESTful API using mocha
  4. Understanding expressjs middleware with a visual example
  5. Setup passportjs for local authentication and authorization using expressjs