Debug JS code inside function node

Hey Steve,
Thanks a lot for the tip :+1: :+1: :+1: :+1:

At the time being this was not possible. But seems that since NodeJs version 8.4.0 it is indeed possible to attach a remote debugger to a vm context. What a pity that I haven't tried it anymore meanwhile...

Here are the steps to debug a function node via the Chrome debugger:

  1. You need to configure ONCE the IP address of your Node-RED server, via chrome://inspect:

    image

    A. Click the 'Configure' button
    B. Enter <ip address Node-RED server>:9229
    C. Click the "Done" button

    Remark: if you are running Node-RED on multiple devices, you can specify here all their IP addresses. But I wouldn't do that because - when one of the Node-RED instances is not accessible - Chrome will keep trying to access it, resulting in long waiting times...

  2. Enter a debugger; statement in your function node code:

    image

  3. Start your Node-RED server in debug-mode (which means it is allowed to attach a debugger process), e.g. on Linux:

    node  --inspect=0.0.0.0  /usr/bin/node-red
    

    Remark: for those who don't know where there Node-RED has been installed, you can find it like this on Linux:

    image

    Remark: for those who want to debug the startup code of Node-RED, you need to use this command:

    node  --inspect=0.0.0.0 --inspect-brk  /usr/bin/node-red
    
  4. Node-RED will start now, and NodeJs allows a debugger process to attach:

    image

  5. Now Chrome should see that the debugger can be attached:

    image

    A. Chrome will show your server
    B. Chrome will show an "inspect" link

  6. After clicking the "inspect" link, a Chrome developer console window will open. You will arrive in the debugger at your function node:

    Remark: it could be that the debugger stops at another debugger statement of another node (e.g. another developer forgot to remove a debugger statement). Just use F8 to unpause the debugger and let it run to the next debugger statement (or breakpoint).

  7. Now you can step through your code (via the navigation buttons on the top right), show content of variables (on the lower right), and so on...

    Remark: it is also possible to debug other nodes, by pressing Ctrl-P, and then selecting the node that you need:

    image

  8. Click on a line number to add a breakpoint, where your debugger needs to stop:

    image

Although Visual Studio code offers much more functionality, for lots of users it might be easier to start with the Chrome debugger I think ...

Have fun with debugging your function node!
Bart

7 Likes