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.
Within a such dynamic runtime, the console role is two-fold:
- 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)
- 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.
Console as the missing part of Node.js core
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.
npm install node-console
Then, to enable it for your Node.js process or application, just include the following code in your source.
// 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.
You can now access the console web interface with your browser at http://127.0.0.1:9090/.
You can now have fun with Node.js console thanks to node-console module.
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).
Troubleshooting Node.js console
- 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.
// 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.