How do I debug a Node.js server application?
Right now I'm mostly using alert debugging with print statements like this:
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?
Install it with:
npm install -g node-inspector
node --prof ./app.js
node --prof-process ./the-generated-log-file
Libraries that output debugging information
Libraries that enhance stack trace information
These use to work but are no longer maintained or no longer applicable to modern node versions.
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
Node.js version 0.3.4+ has built-in debugging support.
node debug script.js
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.
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
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.
A lot of great answers here, but I'd like to add my view (based on how my approach evolved)
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.
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.
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!).
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
If you feel like watching a video(s) then
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
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.
npm install iron-node -g;
If you are using the Atom IDE, you can install the
Using Chrome Version 67.0.3396.62(+)
node --inspect-brk=0.0.0.0:9229 server.js(server js filename)
There will be another DevTools window that will pop out specifically for debugging node app.
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()
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
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
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:
IntelliJ works wonderfully for Node.js.
In addition, IntelliJ supports 'Code Assistance' well.
There are many possibilities...
Nodeclipse is free open-source software released at the start of every month.
New Feature Highlights
Node.js Application Development
- New Node.js project wizard
- New Node.js Express wizard
- New support for running Node.js applications
- New support for debugging Node.js applications.
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
A quick-and-dirty way to debug small Node.js scripts with your favorite browser debugger would be to use browserify. Note that this approach doesn't work with any applications which require native I/O libraries, but it is good enough for most small scripts.
$ npm install -g browserify
Now move all your
var x = requires('x') calls into a
requires.js file and run:
$ browserify requires.js -s window -o bundle.js
(The downside here is that you either have to move or comment the
requires in all your files.)
bundle.js in an HTML file like so:
Now load the file in your browser and press F12 and viola: debug in browser.
©2020 All rights reserved.