How do I debug Node.js applications?

539,239

Solution 1

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.

Solution 2

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

Solution 3

Debugging

Profiling

  1. node --prof ./app.js
  2. node --prof-process ./the-generated-log-file

Heapdumps

Flamegraphs

Tracing

Logging

Libraries that output debugging information

Libraries that enhance stack trace information

Benchmarking

Other

Legacy

These use to work but are no longer maintained or no longer applicable to modern node versions.

Solution 4

Node has its own built in GUI debugger as of version 6.3 (using Chrome's DevTools)

Nodes builtin GUI debugger

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.

Solution 5

Node.js version 0.3.4+ has built-in debugging support.

node debug script.js

Manual: http://nodejs.org/api/debugger.html

Share:
539,239
Fabian Jakobs
Author by

Fabian Jakobs

Updated on March 19, 2022

Comments

  • Fabian Jakobs
    Fabian Jakobs about 2 years

    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?

  • Fabian Jakobs
    Fabian Jakobs over 13 years
    Do you have any links to documentation of how to use it?
  • JulianW
    JulianW over 13 years
    I don't have any docs. just updated to v0.3.5. put a line "debugger;" in your code which will act as break point. It works like ndb / gdb. after you do "node debug script.js" type help. u will see the command it support. p = print, l = list... so you don't need to type the full world
  • balupton
    balupton about 13 years
    I'm interested, after the presentation at Google IO that Paul Irish and Pavel did is it now possible to debug node.js straight to Chrome Developer Tools without the need for eclipse?
  • Marc
    Marc over 12 years
    Note, under windows it's "node.exe --debug myscript.js" but it still don't work.
  • benekastah
    benekastah over 12 years
    You probably have to change --debug to debug without the dashes. That's how I finally got it to work. It's confusing that --debug and debug do two different things.
  • Jonathan Dumaine
    Jonathan Dumaine over 12 years
    Wish node-inspector was active. The profiling component needs to get some love.
  • amateur barista
    amateur barista over 12 years
    +1 Worked very well for me. Using a fresh Eclipse 3.x, x64 version on Mac OS X. The installation instructions are well written as well. Thank you.
  • Silviu-Marian
    Silviu-Marian almost 12 years
    You can use github.com/ketamynx/node-codein instead of node-inspector's console. It can display objects.
  • jayarjo
    jayarjo almost 12 years
    For me breakpoints do not work in node-inspector. Do they for you guys? Any additional mambo-jumbo to make them work?
  • balazs
    balazs over 11 years
    It used to work for me, but with node v0.8.3 it doesn't work :(
  • Leastrio
    Leastrio over 11 years
    @jayarjo is it because your program is exiting before breakpoints can be attached? Have you tried running your code with `node --debug-brk"? This breaks on the first line of the code
  • Leastrio
    Leastrio over 11 years
    webstorm does have snippet support BTW ;-) though they're known as "Live Templates" instead of snippets.
  • Justin Warkentin
    Justin Warkentin over 11 years
    Unfortunately for me, node-inspector doesn't work with the latest versions of Node.js and it hasn't supported logging to the browser console since v0.1. node-codein was just buggy. So, I wrote my own module to help with debugging by allowing you to dump objects and such out to your web browser console. I thought it may be of use to someone else: node-monkey. Plus it works in both Firefox AND Chrome.
  • PeterT
    PeterT about 11 years
    Since this was such an apparently amazing and popular tool, surely the fact that the original author has admitted they no longer have the resources to maintain it wouldn't be a problem as the open source community could pick it up?
  • Juan Lanus
    Juan Lanus almost 11 years
    I'm using node-inspector with node 0.10 and it works as expected. I do it in Ubuntu 13.
  • Paul Verest
    Paul Verest almost 11 years
    Also comes within Nodeclipse nodeclipse.org (with some Node.js related bugs fixed)
  • balupton
    balupton almost 11 years
    Now inspector is now actively maintained by StrongLoop and is working again with the latest version (0.3) yay! Announcement here: blog.strongloop.com/…
  • sidonaldson
    sidonaldson almost 11 years
    if you get an INUSE error try changing the port the inspector runs on: --web-port=8181
  • reallynice
    reallynice over 10 years
    About Nodetime: for those who don't want to send their data to nodetime servers there's a local "alternative" (it's still based on nodetime), the look module, as pointed out in stackoverflow.com/questions/12864221/nodejs-memory-profiling
  • Dan Dascalescu
    Dan Dascalescu over 10 years
    node-inspector was already mentioned; maybe delete this answer?
  • misaxi
    misaxi over 10 years
    this is pretty cool, still don't know what's Backtrace for tho
  • Adam Lear
    Adam Lear about 10 years
    Can you elaborate on how you use this to debug node.js? Thanks!
  • Doron Segal
    Doron Segal about 10 years
    yeah, here's an example: var hapi = require('hapi'); var good = require('good'); var server = hapi.createServer('localhost', 5000,{}); server.route({SOME ROUTE HERE}); server.start(); var options = { subscribers: { 'console': ['ops', 'request', 'log', 'error'], 'localhost/logs': ['log'] } }; server.pack.require('good', options, function (err) { if (!err) { console.log('Plugin loaded successfully'); } });
  • Josh Liptzin
    Josh Liptzin almost 10 years
    If you just want to debug a node.js app and already have an Intellij IDEA license you can just install the node.js plugin without having to buy the WebStorm license. Setting up a run/debug config is very easy once the plugin is installed.
  • Cihad Turhan
    Cihad Turhan almost 10 years
    Debugging both client and server on the same window. What else a web developer wants? This is perfect.
  • shacharz
    shacharz almost 10 years
    I don't find the cpu reports from nodetime very helpful: 1. I just get a tree of methods, with no 'self' time. 2. Seems like the tree branches are trimmed below a certain number of precentage. Those 2 makes it very difficult to undestand where the cpu spends most of its time.
  • Jonathan
    Jonathan almost 10 years
    @DoronSegal It would be a lot easier to read this if you added it to the answer.
  • Doron Segal
    Doron Segal almost 10 years
    Sorry about it, feel free to ping on github if you guys have any questions related to node.
  • keithl8041
    keithl8041 over 9 years
    I have to echo the comments above and say that this tool is far, FAR better than what I was expecting. Definitely give it a go if you haven't tried it before and don't yet have a proper IDE sorted. I've just picked up Node and this is a great tool - works fine with the latest version under Windows (v0.10.31 as of today).
  • Stepan Yakovenko
    Stepan Yakovenko over 9 years
    npm install -g profiler complains about missing python on windows 7. I tried to set python=C:\Python34\, but this gives a crash.
  • Stepan Yakovenko
    Stepan Yakovenko over 9 years
    The only profiler working out of the box is nodetime. But its cpu profiling stacktrace is unusable (it doesn't give enough details). Nodejs tools 4 msvc 2012 also have profiler, but it also has reported critical unfixed bug...
  • Dan Abramov
    Dan Abramov over 9 years
    The only profiler that worked for me was nprof + v8.log from node --prof.
  • rocky
    rocky about 9 years
    My entry into this arena is trepanjs (npmjs.com/package/trepanjs). It has all of the goodness of the node debugger, but conforms better to gdb. It also has more features and commands like syntax highlighting, more extensive online help, and smarter evaluation. See github.com/rocky/trepanjs/wiki/Cool-things for some of its cool features.
  • Mahn
    Mahn almost 9 years
    Is it possible to attach/deattach this debugger during runtime? I guess not, but it would be handy to debug minor issues in production apps without taking them down or running them under the debugger constantly.
  • Jacob McKay
    Jacob McKay almost 9 years
    For most people asking this question, including the OP, this answer should be the accepted answer. It is an outstanding alternative to "alert-debugging" and I've used it a crap-ton with much success :D
  • Eugene
    Eugene almost 9 years
    I'm currently loving Theseus, but I still have some problems where I need to set a breakpoint and trace through. I'm currently having to kill my app, start node with --debug, trace trhough and then start the app with node-theseus. Is it possible to use Theseus with breakpoints? I've tried searching around the GitHub page, StackOverflow and forums, but with no luck so far. Am I missing something?
  • bgse
    bgse over 8 years
    Starting from Visual Studio Code 8.0 the debugging support for OSX and Linux got really good.
  • bgse
    bgse over 8 years
    See the answer @hans posted below, should probably include Visual Studio Code here.
  • Teoman shipahi
    Teoman shipahi over 8 years
    Cloud 9 is way to go for me, especially gives freedom of code anywhere option without carrying my laptop.
  • dashambles
    dashambles over 8 years
    After spending a whole evening getting node-inspector and strongloop to function under windows (Visual Studio Community, downgrade to npm 2, installing python, env variables, use cmd not babun / cygwin etc. etc.) and then playing with this for an hour, I have to say this is the best option at least in windows and possibly in general (if you don't have webstorn)
  • Aditya M P
    Aditya M P about 8 years
    Yes the rumours about it's death are exaggerated. Those comments ought to be removed because it made me disbelieve the answer and waste some more time.
  • T.J. Crowder
    T.J. Crowder about 8 years
    Sadly the explanation seems not to exist on the new wiki page.
  • zeronone
    zeronone almost 8 years
    The feature is currently available in the nightly versions. Check out here for instructions: https://medium.com/@paul_irish/debugging-node-js-nightlies-w‌​ith-chrome-devtools-‌​7c4a1b95ae27#.fitvua‌​umt
  • Michael
    Michael almost 8 years
    How do you get the program to actually run? "r -> app is already running...", when i try to continue and i run into a statement that is trying to get input it drops me back at the debug prompt instead of letting me enter the input that is required.
  • Alister
    Alister over 7 years
    As of Node 6.3 theres a built in node inspector, please see my answer for details...
  • Jarid R. Margolin
    Jarid R. Margolin over 7 years
    Not to discount the steps above, but just to share... I attempted to create a wrapper that is slightly more robust, as well as easier to install. See: github.com/jaridmargolin/inspect-process
  • gregers
    gregers over 7 years
    @JaridR.Margolin Nice. I updated the answer to use that instead. A lot easier to setup and works better :)
  • LOAS
    LOAS over 7 years
    This answer is currently at the bottom and it's the only one that has actually worked for me. This is flipping awesome!
  • MalcolmOcean
    MalcolmOcean about 7 years
    For those using nodemon (if you're not, look it up!), use nodemon --inspect and then this tool to automatically open a new Chrome window: chrome.google.com/webstore/detail/nim-node-inspector-manage/‌​…
  • RoccoB
    RoccoB about 7 years
    In case it helps anyone, I threw up a video explaining this process at youtu.be/rtZKUnks6jI.
  • calbertts
    calbertts about 7 years
    It's cool but it has lag. That's why I do prefer Sublime ever.
  • ThisClark
    ThisClark almost 7 years
    "Since version 6.3, Node.js provides a buit-in DevTools-based debugger which mostly deprecates Node Inspector, see e.g. this blog post to get started. The built-in debugger is developed directly by the V8/Chromium team and provides certain advanced features (e.g. long/async stack traces) that are too difficult to implement in Node Inspector." - says the node inspector repo
  • Pieter Meiresone
    Pieter Meiresone over 6 years
    Where did you got the dark theme for the chrome developer tools?
  • Syed Faizan
    Syed Faizan over 6 years
    but sublime doesn't have a debugger,and i think VS code is pretty fast too
  • Andres Scarpone
    Andres Scarpone over 6 years
    For node 6.x, use node --inspect --debug-brk server.js to automatically stop on the first line.
  • elboletaire
    elboletaire about 6 years
    I've a sublime license since 5 years ago. Since a few months ago I don't even install Sublime Text, just vscode. Out of the box has a lot of tools which I miss in Sublime (like the integrated terminal..).
  • carkod
    carkod almost 6 years
    always asking me for a config folder, it doesn't work out of the box
  • Isaac Pak
    Isaac Pak over 5 years
    debug has been deprecated. you have to use node inspect script.js now
  • akatran
    akatran over 5 years
    Does not work with Netbeans! As node.js changed the option from --debug[-brk] to --inspect[-brk] the only workaround was to add a npm script for quick debugging. Have in package.json something like: "scripts": { "debug": "node --inspect-brk=9292 main.js" }
  • Shivam
    Shivam over 5 years
    Good is not for debugging. Its a hapi js plugging to provide interaction with various events generated by the hapi server.
  • Doron Segal
    Doron Segal over 5 years
    @shivam you are right I wrote this post almost 5 years ago (today Dec, 2018) If you need to debug a node app use vscode with debugger
  • Luciano Mammino
    Luciano Mammino about 5 years
    Definitely worth mentioning node clinic at this point: github.com/nearform/node-clinic
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com almost 5 years
    Tested in node.js v10.15.1. Any way to make it continue automatically to the first debugger without entering c?
  • Vipul Dessai
    Vipul Dessai over 4 years
    @carkod enable vs code auto attach preference and use the vs code terminal to start your script, ex node --inspect file-name.js
  • Jorge Orpinel Pérez
    Jorge Orpinel Pérez over 4 years
    Keep in mind that you need to run nod with --inspect-brk INSTEAD OF --inspect if you want to debug the actual server code at load time. See stackoverflow.com/questions/59596138
  • Jorge Orpinel Pérez
    Jorge Orpinel Pérez over 4 years
    Keep in mind that you need to run nod with --inspect-brk INSTEAD OF --inspect if you want to debug the actual server code at load time. See stackoverflow.com/questions/59596138
  • hongsy
    hongsy over 4 years
    Please consider adding some explanation and details to your answer. While it might answer the question, just adding some links does not help OP or future community members understand the issue or solution.