47

I've installed node-inspector just to find out that it doesn't support breakpoints :| What's the point in it at all, bearing in mind that on big part node code is asynchronous and you simply cannot follow it step by step?..

I'm definitely missing a point here...

Anyway to debug node code with breakpoints and everything?

asked Jul 23, 2012 at 10:56
5
  • Possible duplicate: stackoverflow.com/questions/1911015/… Commented Jul 23, 2012 at 11:02
  • I came here from there - breakpoints do not work in node-inspector and there is no answer to my question. Is there? Commented Jul 23, 2012 at 11:15
  • 1
    It does support breakpoints. I just doesn't 'remember' them when you referesh the inspector page. Commented Jul 23, 2012 at 14:45
  • Possible duplicate of How do I debug Node.js applications? Commented Aug 4, 2019 at 14:01
  • Debugging node.js applications has become much better these days. So this question and answers are probably not even actual anymore. Commented Aug 4, 2019 at 16:25

5 Answers 5

27

yupp, I've successfully used node-inspector. If you want permanent breakpoints, simply insert debugger; in your code. See http://nodejs.org/api/debugger.html.

Making node wait until a debugger is attached, using node --inspect-brk script.js (previously node --debug-brk script.js), can also be very helpful.

Ciro Santilli OurBigBook.com
392k120 gold badges1.3k silver badges1.1k bronze badges
answered Jul 23, 2012 at 12:29
Sign up to request clarification or add additional context in comments.

3 Comments

with debugger; it crashes: node(31848,0x7fff70e12cc0) malloc: *** error for object 0x10010f690: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug Abort trap
I've never seen that error, sorry. Is your app's process crashing, or is it node-inspector? Which versions are you running? Are you using any native node modules that could be crashing?
For later versions of Node.js, --debug-brk is deprecated. Use --inspect-brk instead.
21

(For Node 8 and later)

Node.js has a built-in debugger. Normally you can turn on the debugger in two ways:

  1. Start your Node.js app or script with the --inspect or --inspect-brk switch. For example:

    $ node.js --inspect index.js

(Note: --inspect-brk breaks before user code starts)

  1. If for some reason you cannot start your Node.js app or script with the --inspect switch, you can still instruct the Node.js process to start listening for debugging messages by signalling it with SIGUSR1 (on Linux and OS X). For Node 8 and later it will activate the Inspector API, same as the --inspect switch

    $ kill -sigusr1 23485

(Note: you need to replace 23485 with your own Node.js process ID)

With the debugger turned on, you can open the Google Chrome browser, and type in the address bar chrome://inspect

Then you should see an entry listed under "Remote Target". Go ahead and click "inspect".

Now you can set breakpoints and start debugging your code.

Reference:

  1. https://nodejs.org/en/docs/guides/debugging-getting-started/
  2. Related issue on stackoverflow: Chrome Devtools Dedicated Node.js Inspector not stopping at breakpoints
answered Jun 21, 2018 at 15:23

2 Comments

To do everything in the terminal, you need node inspect instead as explained at: stackoverflow.com/a/55429798/895245 as of v10.15.1.
I've made a video which shows the inspect setup with Node.js (and TypeScript if needed): youtube.com/watch?v=bV-DHjmwuZ0
11

To debug a Node.js application, one can use the debugging built-in method:

(1) Insert debugger; statement where you want to insert a break point
(2) Run the file with command $ node inspect <file name>
(3) Use a key for example, c to continue to next break point

You can even debug values associated to variables at that break point by typing repl. For more information, Please check the official guide.

Cloud Cho
1,82024 silver badges33 bronze badges
answered Mar 30, 2019 at 9:01

2 Comments

Tested on v10.15.1.
If you don't want to modify your code, you can use the function setBreakpoint or sb to set up a breakpoint, eg setBreakpoint('script.js', 1) (nodejs.org/api/debugger.html)
2

Have you tried using nodemon library? it can be found here.

For development purposes you could start the app running nodemon. I have this script:

"dev": "nodemon --inspect src/index.js"

It will break any time a debugger statement is reached in the code. To open the console where you can see the server code, open the console in chrome and click on the nodejs icon:enter image description here

It also helps you refreshing the server every time you save any file on the server.

Let me know if it works!

answered Jun 7, 2019 at 23:00

Comments

1

Just to elaborate a bit here:

Set a debugger wherever you want the breakpoints to be and then run your code with node debug script.js/index.js

When the debugger stops at you breakpoint, you will need to repl to inspect the variables.

answered Apr 29, 2014 at 17:32

2 Comments

i want a breakpoint wherever an exception happens (let's say I don't know ahead of time where that will be). how do i do that?
you can instead of using REPL command add runtime watchers, just type for ex. watch('count') watchers api will print the value when debugger stops in that function.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.