Console: handling Javascript Node.js runtime like a pro

Marco Minetti | Sep 06, 2015

nodejs blink tools webkit javascript npm inspector iot console
Article Goal

This article explains the Javascript console from both a learning and professional perspective.

You will get the tools, some practical tips and must-have links to master Javascript console for browsers and, more important, Node.js with dedicated module.

The open door to the Javascript engine

The common definition of Javascript includes that it "is a high level, dynamic, untyped and interpreted programming language".

If you come from other more "structured" languages (I mean typed and almost compiled like C++, Java, C#), you know that your source code will be compiled into some native or intermediate code before being executed. With those languages, you usually experience the "compile/execute/debug" cycle within a firm, solid, immutable environment (or runtime): you can use a debugger to assess the application state, but if you need to add or change some code, you need to restart - "compile/execute/debug" -  the entire application.

Javascript is different by design.

Javascript source code is dynamically interpreted by the Javascript engine (VM - Virtual Machine) included in Node.js or within your favorite browser. "Dynamically interpreted" means that your code is read and executed live at run-time thus making your application running in an extremely flexible environment: you can change the variables state, add, change or remove code WHILE your application is running.

Within a such dynamic runtime, the console role is two-fold:

  • the console is the open door to interact with the Javascript engine while it's hosting your application;
  • the console is a window to see what is happening inside your application without interrupting its execution.

  

  

Accessing the power of console at client (in the browser)

The Google Chrome browser features some of the best tools for web programming, design and debugging. Those tools includes a fast, lightweight, full-featured and effective console for accessing the Javascript engine at run-time. The Javascript console is accessible using the keyboard shortcut:

  • Command + Option + J (for Mac - a.k.a. Darwin - platform)
  • Control + Shift + J (for Windows and Linux platforms)

The console user interface is steadily updated together with the browser. As the time of writing, it appears as follows.

  

Suggested Tools

I usually recommend Google Chrome as browser. Chrome embedded development tools (a.k.a. formerly WebKit, now Blink tools) are based on Javascript and have been ported to Node.js in must-have tools (node-inspector and node-console). Using Chrome and Node.js, you will get almost the same user interface and experience both for client and server development tools (take a look to screenshots below).

  

Console as the missing part of Node.js core

Node.js is a server side Javascript runtime based on the Chrome Javascript engine. Node.js is lightweight and extremely efficient but it is also very minimalistic.

The "official" un-opinionated way of practicing Node.js involves executing a source file that implements your application that will be loaded into the Javascript runtime at process startup.

If you start a Node.js process without such a startup file, Node.js does actually nothing more than creating an empty Javascript runtime and waiting for some input from the terminal (or command prompt).

  

  

Node.js comes with a REPL command line interface that is perfect to keep everything focused on minimalism and efficiency but it misses an embedded, developer friendly and consistent tool like the Chrome Javascript console.

The solution here is the node-console module available on GitHub and npm. Let's get ready to enable and use the console at server side.

  

Enabling the console at server (in a Node.js process)

The node-console module provides Node.js processes and applications with a console web interface built on the Blink Developer Tools (formerly WebKit Developer Tools).

To enable the Node.js console module you first need to install it from npm, the usual way.

Install node-console module from npm
npm install node-console

Then, to enable it for your Node.js process or application, just include the following code in your source.

Enable the console web interface from Node.js process
// load the node-console module
var web_console = require('node-console');  

// prepare node-console settings
var frontend_port = 9090;
var agent_port = 9999;
var listen_address = '0.0.0.0';

 
// start console interface with defined settings
web_console.start(frontend_port,agent_port,listen_address); 

 
// you can also programmatically stop the console (to allow/restrict access when needed)
//web_console.stop();  

You can also create a new source file to execute just the above code. Assuming you've created a "console.js" file as example, you can launch it as usual.

Install node-console module from npm
node console.js

You can now access the console web interface with your browser at http://127.0.0.1:9090/.

You will get the familiar Chrome console interface but it will be attached to the Node.js process, thus allowing you to handle Javascript runtime at server side from remote clients.

  

  

You can now have fun with Node.js console thanks to node-console module.

Warning

The Node.js console implementation embeds an agent that is loaded inside the node process of your application.

If you pause runtime through breakpoints and debuggers, the console will not work: it is actually part of your running code, not of an isolated debugger.

  

Mastering the Chrome/Node.js Development Tools Console

The Node.js console is built on the official Blink Developer Tools web interface with some slight changes to provide only remote "Console" feature with:

  • multiple front-ends (clients) connections;
  • node logging to front-ends;
  • auto re-attach support on node process restart;
  • programmatically defining of custom ports by URI (to avoid conflicts and to early support multiple back-ends and processes - useful for Node.js cluster applications).

A comprehensive, one-page, and detailed guide on how to use the Javascript console is available at https://developer.chrome.com/devtools/docs/console/ and applies both to browser and Node.js console (with some restrictions).

More advanced features are coming with latest browser releases and can be explored at https://developers.google.com/web/tools/javascript/console/. Remember that Node.js console implementation is community/contribution driven, as the time of writing, and new features may take some time to land into server tools.

Note

Even if Chrome and Node.js share the same Javascript engine (V8), the accessible Javascript runtime has some differences in terms of available - "exposed" - classes, objects and then APIs practices.

  

Troubleshooting Node.js console

Installation failure

Some of my mates had troubles with node.js and node-console installation on Windows platforms. Often problems in Windows are related to:

  • missing or broken C++ headers and build tools: they ship with .NET Framework SDK or Visual Studio, including the Visual Studio Express/Community editions, so make sure you have it and there are no conflicts between different versions;
  • missing Python 2.x executable in the system or in the PATH env variable: open a command prompt and try to execute "python.exe", reinstall it from Python website, if necessary;
  • io.js having problems with npm as the time of writing: use official node.js on Windows please or fix the bugs by yourself;
  • missing or wrong versions of node-gyp and node-pre-gyp: you need to install them manually through the "npm install node-gyp node-pre-gyp -g".

Already used port (and TCP/IP implications)

The node-console module is currently implemented to use two different ports:

  • web port, defaults 9090
  • agent port, defaults 9999

Ensure those ports are available on your system or change it before enabling the console.

Missing "require" function

Remember that the "require" function is not available globally. If you want to use it within the Node.js console, you need to pin it to the global object at the very beginning of your application.

Pin the "require" function to the global object for programmatic usage
// within the node.js startup script
global.require = require;

  

  

If you are experiencing any other problem, please contact me so we can extend this section.

You can also report bugs or feature requests directly at the GitHub project page.

  

Further readings and related articles