1715

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?

Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
asked Dec 15, 2009 at 22:50
7
  • 3
    You can use Locus for command line injection. Commented Dec 12, 2013 at 19:44
  • 6
    If you want to debug with the traditional IDE appoach, use vscode use vscode youtube.com/watch?v=egBJ0cd0GLM Commented Sep 20, 2016 at 3:09
  • 4
    I have found this article very interesting, and it works for me just fine: Debugging Node.js with Chrome DevTools. Hope it helps :) Commented Feb 15, 2017 at 9:50
  • 2
    "alert debugging" :) Commented Jun 1, 2017 at 16:32
  • 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 Commented Jan 5, 2020 at 0:48

42 Answers 42

1
2
1311
+50

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
answered Oct 15, 2010 at 17:17
Sign up to request clarification or add additional context in comments.

17 Comments

Wish node-inspector was active. The profiling component needs to get some love.
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.
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?
Now inspector is now actively maintained by StrongLoop and is working again with the latest version (0.3) yay! Announcement here: blog.strongloop.com/…
"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
|
796

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.

Brian
1,0361 gold badge17 silver badges26 bronze badges
answered May 12, 2013 at 21:37

9 Comments

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
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.
npm install -g profiler complains about missing python on windows 7. I tried to set python=C:\Python34,円 but this gives a crash.
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...
The only profiler that worked for me was nprof + v8.log from node --prof.
|
268

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.

d4nyll
12.8k6 gold badges58 silver badges72 bronze badges
answered Mar 29, 2010 at 8:57

7 Comments

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?
+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.
Also comes within Nodeclipse nodeclipse.org (with some Node.js related bugs fixed)
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.
The feature is currently available in the nightly versions. Check out here for instructions: https://medium.com/@paul_irish/debugging-node-js-nightlies-with-chrome-devtools-7c4a1b95ae27#.fitvuaumt
|
214

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.

answered Sep 14, 2016 at 14:51

8 Comments

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
@JaridR.Margolin Nice. I updated the answer to use that instead. A lot easier to setup and works better :)
This answer is currently at the bottom and it's the only one that has actually worked for me. This is flipping awesome!
In case it helps anyone, I threw up a video explaining this process at youtu.be/rtZKUnks6jI.
For node 6.x, use node --inspect --debug-brk server.js to automatically stop on the first line.
|
97

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

node debug script.js

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

Celmaun
24.9k9 gold badges73 silver badges59 bronze badges
answered Jan 16, 2011 at 1:20

7 Comments

Do you have any links to documentation of how to use it?
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
Note, under windows it's "node.exe --debug myscript.js" but it still don't work.
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.
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.
|
77

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.

enter image description here enter image description here

answered Feb 19, 2016 at 10:21

5 Comments

It's cool but it has lag. That's why I do prefer Sublime ever.
but sublime doesn't have a debugger,and i think VS code is pretty fast too
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..).
always asking me for a config folder, it doesn't work out of the box
@carkod enable vs code auto attach preference and use the vs code terminal to start your script, ex node --inspect file-name.js
61

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:

  1. (削除) It seems to be more resource hungry on Mac than Windows (削除ここまで) It no longer seems an issue in version 6.
  2. (削除) 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.
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered May 3, 2012 at 14:42

2 Comments

webstorm does have snippet support BTW ;-) though they're known as "Live Templates" instead of snippets.
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.
46

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

If you feel like watching a video(s) then

Whatever path you choose, just be sure you understand how you are debugging

enter image description here

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

answered Sep 15, 2015 at 15:33

Comments

44

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.

screenshot

answered May 14, 2013 at 22:21

2 Comments

this is pretty cool, still don't know what's Backtrace for tho
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?
27

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.

Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered May 2, 2014 at 12:30

Comments

25

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...

Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered Sep 9, 2015 at 0:08

2 Comments

Starting from Visual Studio Code 8.0 the debugging support for OSX and Linux got really good.
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)
24

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.

Enter image description here

An opensource cross-platform visual debugger.

Installation:

npm install iron-node -g;

Debug:

iron-node yourscript.js;

answered Jul 19, 2015 at 15:10

Comments

17

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()
keen
86010 silver badges11 bronze badges
answered Dec 9, 2014 at 15:22

Comments

16

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

Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered Mar 30, 2015 at 4:04

Comments

16

Using Chrome Version 67.0.3396.62(+)

  1. Run node app

node --inspect-brk=0.0.0.0:9229 server.js(server js filename)

  1. Browse your app in chrome e.g. "localhost:port"
  2. Open DevTools.
  3. Click the the node icon beside the responsive device icon.

enter image description here

There will be another DevTools window that will pop out specifically for debugging node app.

enter image description here

answered Jun 4, 2018 at 12:21

Comments

13

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

keen
86010 silver badges11 bronze badges
answered Mar 11, 2011 at 19:29

1 Comment

Cloud 9 is way to go for me, especially gives freedom of code anywhere option without carrying my laptop.
11

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

answered Nov 21, 2013 at 18:31

Comments

10

Visual Studio Code will work for us in debugging.

answered Oct 15, 2017 at 10:02

Comments

9

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

answered Jul 25, 2016 at 19:13

Comments

8

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.

Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered Mar 8, 2012 at 1:46

Comments

8

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.

Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered Apr 29, 2013 at 1:20

1 Comment

node-inspector was already mentioned; maybe delete this answer?
8
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered Feb 5, 2014 at 1:27

Comments

8

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

answered Feb 9, 2018 at 19:05

Comments

7

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.

Glorfindel
22.8k13 gold badges97 silver badges124 bronze badges
answered Jan 2, 2014 at 5:24

Comments

6

There are many possibilities...

Debug support is often implemented using the v8 Debugging Protocol or the newer Chrome Debugging Protocol.

answered Jan 21, 2017 at 6:13

Comments

5

IntelliJ works wonderfully for Node.js.

In addition, IntelliJ supports 'Code Assistance' well.

answered Aug 5, 2015 at 3:34

Comments

4

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:

  1. NetBeans Wiki / NewAndNoteworthyNB81.
  2. Node.js Express App in NetBeans IDE, Geertjan-Oracle.
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered Nov 24, 2015 at 8:26

Comments

4

Use this commands

DEBUG_LEVEL=all node file.js
DEBUG=* node file.js
node file.js --inspect
answered Mar 24, 2018 at 1:51

Comments

4

You may use pure Node.js and debug the application in the console if you wish.

For example let's create a dummy debug.js file that we want to debug and put breakpoints in it (debugger statement):

let a = 5;
debugger;
a *= 2;
debugger;
let b = 10;
debugger;
let c = a + b;
debugger;
console.log(c);

Then you may run this file for debugging using inspect command:

node inspect debug.js

This will launch the debugger in the console and you'll se the output that is similar to:

< Debugger listening on ws://127.0.0.1:9229/6da25f21-63a0-480d-b128-83a792b516fc
< For help, see: https://nodejs.org/en/docs/inspector
< Debugger attached.
Break on start in debug.js:1
> 1 (function (exports, require, module, __filename, __dirname) { let a = 5;
 2 debugger;
 3

You may notice here that file execution has been stopped at first line. From this moment you may go through the file step by step using following commands (hot-keys):

  • cont to continue,
  • next to go to the next breakpoint,
  • in to step in,
  • out to step out
  • pause to pause it

Let's type cont several times and see how we get from breakpoint to breakpoint:

debug> next
break in misc/debug.js:1
> 1 (function (exports, require, module, __filename, __dirname) { let a = 5;
 2 debugger;
 3
debug> next
break in misc/debug.js:2
 1 (function (exports, require, module, __filename, __dirname) { let a = 5;
> 2 debugger;
 3
 4 a *= 2;
debug> next
break in misc/debug.js:4
 2 debugger;
 3
> 4 a *= 2;
 5 debugger;
 6

What we may do now is we may check the variable values at this point by writing repl command. This will allow you to write variable name and see its value:

debug> repl
Press Ctrl + C to leave debug repl
> a
5
> b
undefined
> c
undefined
>

You may see that we have a = 5 at this moment and b and c are undefined.

Of course for more complex debugging you may want to use some external tools (IDE, browser). You may read more here.

answered Sep 20, 2018 at 11:20

2 Comments

Tested in node.js v10.15.1. Any way to make it continue automatically to the first debugger without entering c?
Appreciate the instructions here, particularly the need to invoke the repl command to check variable values.
4

ndb is an improved debugging experience for Node.js, enabled by Chrome DevTools

https://github.com/GoogleChromeLabs/ndb

answered Dec 11, 2018 at 19:52

Comments

1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.