How do I debug Node.js applications?

How do I debug Node.js applications?

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?

Solutions/Answers:

Solution 1:

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 2:

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 3:

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

To open a Chrome window automatically, use the inspect-process module.

# install inspect-process globally
npm install -g inspect-process

# start the debugger with inspect
inspect script.js

Solution 5:

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

node debug script.js

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

Solution 6:

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