I had what started off as a simple function node where the code has gotten much more complex as I have been addressing a variety of corner cases. Unfortunately all of that complexity has hidden some bugs. I really need to be able to step through the code to sort it out. Ideally I would be able to use the Chrome debugger, but I found this from quite some time ago:
Is that an accurate sumation of what is possible?
I had been contemplating at some point actually building my own custom nodes and this may push me over the edge to do it, but I such a long to do list, I wasn't jumping at the chance to go there yet.
The function node runs server side not client side. While it is still possible (to use Chrome Dev tools) what I do is start node-red via VSCode then when I want to debug a function node, I write a debugger statement in the function and Vs code stops on that like & I can step through.
Which is more likely a smaller learning curve, going down the custom node path (which I will want to do eventually anyway) or learning how to start node-red via VSCode?
After quickly skimming this, I had one question. Does this require NR to be installed and running on the machine running VS Code?
Or perhaps would this work with the existing install by altering this line:
"program": "c:\path_to_npm_global\node-red\red.js"
I currently have NR running in a docker container on a NAS and am wondering if I am about to have more than one NR environment and have to start importing/exporting to get the environments to look similar.
You'll likely have to start node with --inspect flag and expose port 9229 to outside docker then use chrome dev tools
node --inspect path_to/node-red/red.js
I'm certain you could also attach VSCode (but I haven't done it myself)
Search the forum for "@BartButenaers docker debug" - i'm certain i seen something along the lines of debugging a docker install not too long ago.
PS, you should have stated docker earlier - :sigh: - might be worth updating the title to something like "Debug function node remotely when running node-red in docker"
The way I debug javascript for function nodes is by using REPL.IT
This gives you an online REPL environment that allows you to test and debug javascript code.
Sure, you may have to hard code a msg.payload or other variables you may be using in your flow, but after that it's pretty great, as you can made incremental changes in the repl, and simulate whatever permutations you cant without affecting your flow in node-red, and once you're happy, just copy your code over to your function node and deploy. Plus you get the power of the REPLs linting and formatting as well (right click in the editor).
I wish I had discovered it earlier!
Heuh can you debug a function node? At the time being, I realised it wasn't possible because the javascript was passed by NodeJs to a sandbox as a single string, which 'evaluated' that string in a single statement. So that was my trigger to put my code in my first custom node, because that was easily to debug... But would be nice if it is possible now!
Absolutely. The sandbox code is shown only but you can inspect the value of objects like flow and global and msg etc. Works a treat. I use it when I can't see the trees for the wood. Step through code, see where I messed up.
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:
You need to configure ONCE the IP address of your Node-RED server, via chrome://inspect:
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...
Enter a debugger; statement in your function node code:
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:
Remark: for those who want to debug the startup code of Node-RED, you need to use this command:
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).
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:
Click on a line number to add a breakpoint, where your debugger needs to stop:
Although Visual Studio code offers much more functionality, for lots of users it might be easier to start with the Chrome debugger I think ...
It has been done already
But for people starting the topic with the pen icon on the first post that also allows to change title of the topic. (Since this is in FAQ, have some more FAQ)