How do I debug Node.js applications? - javascript

How do I debug a Node.js server application?
Right now I'm mostly using alert debugging with print statements like this:
sys.puts(sys.inspect(someVariable));
There must be a better way to debug. I know that Google Chrome has a command-line debugger. Is this debugger available for Node.js as well?

node-inspector could save the day! Use it from any browser supporting WebSocket. Breakpoints, profiler, livecoding, etc... It is really awesome.
Install it with:
npm install -g node-inspector
Then run:
node-debug app.js

Debugging
Joyent's Guide
Debugger
Node Inspector
Visual Studio Code
Cloud9
Brackets
Profiling
node --prof ./app.js
node --prof-process ./the-generated-log-file
Heapdumps
node-heapdump with Chrome Developer Tools
Flamegraphs
0x
jam3/devtool then Chrome Developer Tools Flame Charts
Dtrace and StackVis — Only supported on SmartOS
clinicjs
Tracing
Interactive Stack Traces with TraceGL
Logging
Libraries that output debugging information
Caterpillar
Tracer
scribbles
Libraries that enhance stack trace information
Longjohn
Benchmarking
Apache Bench: ab -n 100000 -c 1 http://127.0.0.1:9778/
wrk
Other
Trace
Vantage
Bugger
Google Tracing Framework
Paul Irish's Guide
Legacy
These use to work but are no longer maintained or no longer applicable to modern node versions.
https://github.com/bnoordhuis/node-profiler - replaced by built-in debugging
https://github.com/c4milo/node-webkit-agent - replaced by node inspector
https://nodetime.com/ - defunct

The V8 debugger released as part of the Google Chrome Developer Tools can be used to debug Node.js scripts. A detailed explanation of how this works can be found in the Node.js GitHub wiki.

Node has its own built in GUI debugger as of version 6.3 (using Chrome's DevTools)
Simply pass the inspector flag and you'll be provided with a URL to the inspector:
node --inspect server.js
You can also break on the first line by passing --inspect-brk instead.

Node.js version 0.3.4+ has built-in debugging support.
node debug script.js
Manual: http://nodejs.org/api/debugger.html

Visual Studio Code will be my choice for debugging. No overhead of installing any tools or npm install stuff.
Just set the starting point of your app in package.json and VSCode will automatically create a configuration file inside your solution. It's build on Electron, on which editors like Atom are built.
VS Code gives similar debugging experience as you might have
had in other IDEs like VS, Eclipse, etc.

I personally use JetBrains WebStorm as it's the only JavaScript IDE that I've found which is great for both frontend and backend JavaScript.
It works on multiple OS's and has Node.js debugging built-in (as well as a ton of other stuff](http://www.jetbrains.com/webstorm/features/index.html).
My only 'issues'/wishlist items are were:
It seems to be more resource hungry on Mac than Windows It no longer seems an issue in version 6.
It would be nice if it had Snippet support (like those of Sublime Text 2 - i.e. type 'fun' and tap 'tab' to put in a function. See #WickyNilliams comment below - With Live Templates you also have snippet support.

A lot of great answers here, but I'd like to add my view (based on how my approach evolved)
Debug Logs
Let's face it, we all love a good console.log('Uh oh, if you reached here, you better run.') and sometimes that works great, so if you're reticent to move too far away from it at least add some bling to your logs with Visionmedia's debug.
Interactive Debugging
As handy as console logging can be, to debug professionally you need to roll up your sleeves and get stuck in. Set breakpoints, step through your code, inspect scopes and variables to see what's causing that weird behaviour. As others have mentioned, node-inspector really is the bees-knees. It does everything you can do with the built-in debugger, but using that familiar Chrome DevTools interface.
If, like me, you use Webstorm, then here is a handy guide to debugging from there.
Stack Traces
By default, we can't trace a series of operations across different cycles of the event loop (ticks). To get around this have a look at longjohn (but not in production!).
Memory Leaks
With Node.js we can have a server process expected to stay up for considerable time. What do you do if you think it has sprung some nasty leaks? Use heapdump and Chrome DevTools to compare some snapshots and see what's changing.
For some useful articles, check out
RisingStack - Debugging Node.js Applications
Excellent article by David Mark Clements of nearForm
If you feel like watching a video(s) then
Netflix JS Talks - Debugging Node.js in Production
Interesting video from the tracing working group on tracing and debugging node.js
Really informative 15-minute video on node-inspector
Whatever path you choose, just be sure you understand how you are debugging
It is a painful thing
To look at your own trouble and know
That you yourself and no one else has made it
Sophocles, Ajax

Theseus is a project by Adobe research which lets you debug your Node.js code in their Open Source editor Brackets. It has some interesting features like real-time code coverage, retroactive inspection, asynchronous call tree.

Node.js Tools for Visual Studio 2012 or 2013 includes a debugger. The overview here states "Node.js Tools for Visual Studio includes complete support for debugging node apps.". Being new to Node.js, but having a background in .NET, I've found this add in to be a great way to debug Node.js applications.

Visual Studio Code has really nice Node.js debugging support. It is free, open source and cross-platform and runs on Linux, OS X and Windows.
You can even debug grunt and gulp tasks, should you need to...

I wrote a different approach to debug Node.js code which is stable and is extremely simple. It is available at https://github.com/s-a/iron-node.
An opensource cross-platform visual debugger.
Installation:
npm install iron-node -g;
Debug:
iron-node yourscript.js;

I created a neat little tool called pry.js that can help you out.
Put a simple statement somewhere in your code, run your script normally and node will halt the current thread giving you access to all your variables and functions. View/edit/delete them at will!
var pry = require('pryjs')
class FizzBuzz
run: ->
for i in [1..100]
output = ''
eval(pry.it) // magic
output += "Fizz" if i % 3 is 0
output += "Buzz" if i % 5 is 0
console.log output || i
bar: ->
10
fizz = new FizzBuzz()
fizz.run()

If you are using the Atom IDE, you can install the node-debugger package.

Using Chrome Version 67.0.3396.62(+)
Run node app
node --inspect-brk=0.0.0.0:9229 server.js(server js filename)
Browse your app in chrome e.g. "localhost:port"
Open DevTools.
Click the the node icon beside the responsive device icon.
There will be another DevTools window that will pop out specifically for debugging node app.

There is built-in command line debugger client within Node.js. Cloud 9 IDE have also pretty nice (visual) debugger.

I put together a short Node.js debugging primer on using the node-inspector for those who aren't sure where to get started.

Visual Studio Code will work for us in debugging.

Use Webstorm! It's perfect for debugging Node.js applications. It has a built-in debugger. Check out the docs here: https://www.jetbrains.com/help/webstorm/2016.1/running-and-debugging-node-js.html

If you need a powerful logging library for Node.js, Tracer https://github.com/baryon/tracer is a better choice.
It outputs log messages with a timestamp, file name, method name, line number, path or call stack, support color console, and support database, file, stream transport easily. I am the author.

Assuming you have node-inspector installed on your computer (if not, just type 'npm install -g node-inspector') you just have to run:
node-inspector & node --debug-brk scriptFileName.js
And paste the URI from the command line into a WebKit (Chrome / Safari) browser.

Just for completeness:
The PyCharm 3.0 + Node.js Plugin offers an awesome development + run + debug experience.

Start your node process with --inspect flag.
node --inspect index.js
and then Open chrome://inspect in chrome. Click the "Open dedicated DevTools for Node" link or install this chrome extension for easily opening chrome DevTools.
For more info refer to this link

There is the new open-source Nodeclipse project (as a Eclipse plugin or Enide Studio):
Nodeclipse became #1 in Eclipse Top 10 NEW Plugins for 2013. It uses a modified V8 debugger (from Google Chrome Developer Tools for Java).
Nodeclipse is free open-source software released at the start of every month.

There are many possibilities...
node includes a debugging utility
node-inspector
Code editors / IDEs (see debug instructions for one of the following)
Atom,
VSCode
Webstorm
and more
Debug support is often implemented using the v8 Debugging Protocol or the newer Chrome Debugging Protocol.

IntelliJ works wonderfully for Node.js.
In addition, IntelliJ supports 'Code Assistance' well.

The NetBeans IDE has had Node.js support since version 8.1:
<...>
New Feature Highlights
Node.js Application Development
New Node.js project wizard
New Node.js Express wizard
Enhanced JavaScript Editor
New support for running Node.js applications
New support for debugging Node.js applications.
<...>
Additional references:
NetBeans Wiki / NewAndNoteworthyNB81.
Node.js Express App in NetBeans IDE, Geertjan-Oracle.

Use this commands
DEBUG_LEVEL=all node file.js
DEBUG=* node file.js
node file.js --inspect

ndb is an improved debugging experience for Node.js, enabled by Chrome DevTools
https://github.com/GoogleChromeLabs/ndb

node-debug -p 8888 scriptFileName.js

Related

Debugging JS and Node.js In Eclipse 2022

Does anyone know of a good guide to debugging a JavaScript file/project using Node.js while using Eclipse 2022? I've seen a few articles but they all relate to previous versions of Eclipse and no longer seem applicable or work.
I can run a simple hello-world.js program at command-line [ node --inspect-brk hello-world.js] and then interact with Chrome's node.js debug tool [chrome://inspect/] but curious if this can all be done within Eclipse.

Embedding Mozilla's JavaScript Engine in C++

I want to embed Mozilla's SpiderMonkey in my standalone C++ program (in Visual Studio 2019). I went over all the documentation and whatnot but the problem is (or what I need is):
Execute a JavaScript script through C++. In pseudo code that'd be Value* result = SpiderMonkeyEvaluate(fileString);
And that's it. The thing is, in the documentation it says I need to build the entirety of Mozilla's Firefox browser (which is not less than a Gigabyte) and download Mercurial and open the command prompt and create directories etc. and all that disgusting stuff. I just need to be able to:
#include <jsapi.h>
And perform all the necessary C++-to-JS (and vice-versa) operations.
I do not think I need to download and build the entire FireFox browser to do this (I maybe wrong?).
If anyone is still interested the spider monkey on Windows, you would follow the build instructions here:
https://firefox-source-docs.mozilla.org/js/build.html
using the build tools here:
https://firefox-source-docs.mozilla.org/setup/windows_build.html
If you are very meticulous you don't need to download the entire FireFox code base but your life will be easier if you do. In order to only build spidermonkey only (plus js command line), be sure to activate a particular MOZCONFIG with
ac_add_options --enable-application=js
in it before calling any of the mach commands.
Note: if you plan to use the Visual C++ compiler and not clang-cl, you are limited to version 78 or you will have to figure out how to modify the header yourself.
for reference this is the config I used to successfully build spidermonkey 81.0.2 on Windows 10:
# Build only the JS shell
ac_add_options --enable-application=js
# Enable optimization for speed
ac_add_options --enable-optimize
# Enable the debugging tools: Assertions, debug only code etc.
# For performance testing you would probably want to change this
# to --disable-debug.
ac_add_options --disable-debug
ac_add_options --disable-jemalloc
ac_add_options --prefix=$MY_PREFIX
mk_add_options MOZ_MAKE_FLAGS="-j4"
# Use a separate objdir for optimized builds to allow easy
# switching between optimized and debug builds while developing.
mk_add_options MOZ_OBJDIR=#TOPSRCDIR#/obj-opt-#CONFIG_GUESS#
If you use this config, after you successfully build,
then navigate to #TOPSRCDIR#/obj-opt-#CONFIG_GUESS# and do mozmake install, it "install" all the files you need into $MY_PREFIX
UPDATE - This answer is wrong. sparse checkout actually downloads all the history. One would have to use the narrowhg ext, which isn't supported on the hg.mozilla.org repo.
the following answer is wrong.
You could use mercurial sparse checkout to just get the js/src folder.
(but with all the history of the js/src folder it will still likely be quite big)
This will just the history for js/src folder.
you need a version of mercurial which is newer then 3 years old (4.3 or newer)
mkdir spidermonkey
cd spirdermonkey
hg init
hg debugsparse --include js/src
hg pull https://hg.mozilla.org/releases/mozilla-release -u

Debugging local nodejs script in chrome dev tools

I have a stand alone node js script.
I am doing some local task with that script. I want to debug that script in chrome dev tools. I know I can debug it locally via putting debugger in code but do not want to do that.
On nodejs docs I saw that it has some options like -
V8 Inspector Integration for Node.js# NOTE: This is an experimental
feature.
V8 Inspector integration allows attaching Chrome DevTools to Node.js
instances for debugging and profiling.
V8 Inspector can be enabled by passing the --inspect flag when
starting a Node.js application. It is also possible to supply a custom
port with that flag, e.g. --inspect=9222 will accept DevTools
connections on port 9222.
To break on the first line of the application code, provide the
--debug-brk flag in addition to --inspect.
$ node --inspect index.js
But when I do that it gives me error like -
$ node --inspect index.js
node: bad option: --inspect
My node version is:
$ node --version
v4.4.7
You need node 6.3 or above --inspect is not supported in 4.4.7
#Andrey
Thanks a lot for pointing out that I was scratching my head for a long time on this.
#dan
Thanks for node-inspector suggestion.
I am just posting an answer in case someone else might stuck on this.
I have installed node-inspector and,
This is what I'm doing to start the debugger..
Opened one terminal and
$ node-inspector --no-preload
Node Inspector v0.12.5
Visit http://127.0.0.1:8080/?ws=127.0.0.1:8080&port=5858 to start debugging.
In another terminal,
$ node --debug-brk app.js
debugger listening on port 5858
Intially I was using just --debug. But it was not hitting the breakpoints and was going through all code. Then I used --debug-brk. Using --debug-brk caused node to break on the first line of app and waited for a debugger to hit breakpoints.
Then started Google Chrome and went to
http://127.0.0.1:8080/debug?port=5858
Here chrome dev tools was opened and I was able to put break point and debug the code.
Node Inspector works pretty well. Also keep in mind Node 7 has native node debugging, even supports live-edit! Here's a gif of it in action: Chrome DevTools: Live edit running Node.js code with hotswapping

How to debug node.js application with Visual Studio 2013 and Node Tools NTVS

I have moved an existing node.js + express project to VS because I prefer the IDE over JetBrains for now (used VS for years, only peeked into Webstorm).
I used NTVS new project->from existing sources and all files were imported successfully.
Afterwards, I opened the project settings of my project and set the node.exe arguments to bin\www, startup file for express.
When I press F5 (debug) I get the console.log messages I have put into the www and app.js files in the opening command prompt, and it looks like the server is running (cannot confirm, I want to debug if everything is working), but the VS debugger directly exits again, it also does not open any page in the browser I selected for debugging.
My node app actually is a REST webservice, so I want to test different URLs with different parameters.
Also, I cannot access the app on the port I specified, though when I directly start it from node.exe I can, even though the command prompt is still open.
(I have NTVS and WebEssentials installed - some operations take a long long time, but I attribute this to NTVS being still an early version.)
Question: how does the Visual Studio debugger stay connected to the node.js application so I can use breakpoints and use any browser then to connect and test different URLs? (Even a breakpoint put on the console.log that gets printed during startup is not being triggered.)
For everyone who asks receives, and the one who searches finds....
(and yes, I did spend a long time searching and trying before posting here..)
Kind of nice to debug node.js server with VS..
hope this helps someone
Edit: The arguments to node.exe can be hard to read in the image. It must be
--debug=<portno>
that is with two dashes (and not just one) to specify the debug port.
Not so much knowledge on expressjs but with a recent release of NTVS 1.0 Alpha, I did find it supports remote debugging which can be also used to debug nodejs app running locally - anyway haven't tried if it works with nodejs app + expressjs but it should.
I followed the step in this video https://youtu.be/-ir9ZB8lUg4 which is
Run your nodejs with node.exe RemoteDebug.js <your_javascript_file>.
RemoteDebug.js has come when you install NTVS.
In Visual Studio, select Debug > Attach to Process
Select Node.js remote debugging for Transport
Enter localhost:5859 for Qualifier
Click Attach
This will put Visual Studio in debugging mode which you can set a breakpoint, do step-in/step-out, very same experience when you use VS to debug .NET app.
Its pretty straight forward with NTVS, you can download required version for your windows from github here
Once you install NTVS, NodeJS project templates will be added
Now, Goto File->New project -> Basic NodeJS Express 3 application (it will be available in javascript project templates)
Now just goto debug and select Start Debugging, add breakpoints where ever required and you can start debugging

Combine c++ and javascript development in Eclipse

I am new to Eclipse IDE, but now I need it to develop apps for webOS.
Help me please with the next questions:
I have downloaded the Eclipse IDE for JavaScript Web development. How can I find it's version information?
Can I use one instance of Eclipse IDE for JavaScript and C++ development at the same time? (if yes then please write the steps to extend JavaScript IDE to support C++ development) Or I should have two instances installed?
Some parts of documentation are written exactly for Eclipse Galileo C++ development. Are different versions of Eclipse IDE really different? Will I have problems porting settings from Galileo to Helios or Indigo?
Thanks.
Usually Versioninformation can be found under Help->About Eclipse
I do not know about this exact setup but I have Java/C++/PHP in one Eclipse install.
I installed the java version and then Help->Install New Software where I choose -all sites- as source and then searched for the compontent I wanted and installed it.
After installation restart eclipse to load the new plugins and now you should have a new Perspective for the language you installed.
If you can not find the perspective look under Window->Open Perspective->Other.
I do not know although I work with different version. Maybe some names (for example in menus) are changed but I really can't say (maybe a hint that they are not different? But Probably I just did not dive that deep into it).
The menu-paths I gave are under Indigo release.
I'm using Eclipse CDT with Aptana as a plugin which covers both bases for me.

Categories

Resources