How to debug an own node with the node.js debugger?

Hey @noir

You will need to use both command arguments to let Node-RED halt at the first line AND make sure that from then on your debugger can communicate with NodeJs:

node --inspect --debug-brk /usr/lib/node_modules/node-red/red.js

Will add some extra explanation below, for those who want to debug their code using Chrome...

Some remarks:

  • Note that you cannot debug code in function nodes! The code in a function node is one single piece of text, that is handed over to NodeJs to execute as one whole. I wrote some extra explanation in the past about it ...
  • Probably Visual Studio Code is much more powerful, but I debug using Chrome because it is simple and offers everything I need ...
  • When you search on the web, you will also come across a node-inspector module. In the beginning NodeJs had a dedicated debug protocol, so you needed that browser plugin to connect your browser debugger to NodeJs. However now NodeJs supports a standard debug protocol, so browser like Chrome can directly communicate with NodeJs without plugins. So we won't use that technology in this discussion ...

I use Chrome both to debug:

  • The client-side of my node (which runs in my browser like the Dashboard or the Flow editor), i.e. the Javascript used for my node's config screen. For example stuff from the oneditprepare function.
  • The server-side of my node (which runs in my flow on my Raspberry), i.e. the code that is e.g. triggered when a new input message arrives.

As mentioned before the technology (to debug server-side NodeJs from Chrome) is experimental, so it changes from time to time how it has to be used:

  • In older NodeJs versions you had to startup Node-RED (on a Raspberry for example) like this:

    node --inspect /usr/lib/node_modules/node-red/red.js
    

    Then in the command line an URL will be displayed automatically:

    Chrome-devtools:// ...
    

    You have to copy that URL and paste in your Chrome browser. And then the developer tools opens and you can start debugging. Additionally you can add extra --debug-brk in your command (after --inspect), so it would stop immediately at the first line of the Node-RED application. This is e.g. necessary when you want to debug code from Node-RED startup, or the startup code of your node. E.g. this part of your Node:

    function MyCustomNode(config) {
         RED.nodes.createNode(this, config);
         // This startup code needs to be debugged (e.g. after deploy)
    }
    

    This way of working gives you a change to put your breakpoints before the flow is started...

  • Starting from starting from NodeJs version 7.5.0, you need to specify extra in your command which remote ip address is allowed to debug (see this security issue in NodeJs)):

    node --inspect=0.0.0.0:5858 /usr/lib/node_modules/node-red/red.js
    

    So that is the IP address of the computer where you are running Chrome. I 'think' you could use 0.0.0.0 to allow all IP addresses ...

  • Starting from NodeJs version 8.?, the command displays a ws://... address instead of chrome-devtools://... address. To open such an URL with Chrome you have to follow these steps:

    • Navigate to chrome://inspect

    • Once you have to specify the IP address of your server (in my case the Raspberry running my Node-RED) via the 'Configure' button:

      image

      You can specify here the IP addresses of ALL your Node-RED servers, that you ever want to debug...

    • Once you start one of those servers in debug mode (i.e. with the --inspect ... command), those entries will become clickable in this screen:

      image

      If you don't see the link appearing, refresh this page.

    • When you click on the 'inspect' link, the developer console will open and you can start debugging...

I'm only using the latest mechanism, so I cannot test the others anymore for you (in case of troubles)!

Bart

5 Likes