Debugging function

Just started to use function node. Curious how do you debug the code step by step? Or how to log values to console/debug window or to a file?

Thanks.

Please refer to the documentation.

"step by step" can be done by adding debug nodes to all the nodes.

1 Like

There is no log to file capability native to Node-RED. However, the debug node can be made to output to the Node-RED log and that log could be redirected to a file. If running a standard install on a Pi for example, you likely have NR running at startup. As such, the log file is already written to file and can be accessed by a number of tools or indeed anything that will read a text file.

In the past I've also written flows that output to a web page which you might also find useful. Check out the flows section of the site.

1 Like

If you want to debug the code you have in the function node, insert
node.warn("your_variable="+your_variable);
I like to make mine look like this
node.warn("dbg01: your_variable="+your_variable);
so I can find where I put them. The results will display in the debug sidebar

3 Likes

Recently I discovered a alternative method useful for inspecting objects (and arrays):

node.warn(["your_variable=",your_variable]);

You will get a single nice expandable entry into your debug window for as many variables from any kind you like.

Or as an object:

node.warn({"your_variable":your_variable,"msg":msg});

Example:

contextStore.pushValueToChart = function (chart,serie,label,value,maxTime,maxMsgs) {
  node.warn(["pushValueToChart",chart,serie,label,value,maxTime,maxMsgs])
  // more code
}

only copy paste all arguments of the function into the array passed to node.warn (+text to make it easier to identify the source)

image

but you have to remember the variable names and order
or as an object:

node.warn({"function":"pushValueToChart","chart":chart,"serie":serie,"label":label,"value":value,"maxTime":maxTime,"maxMsgs":maxMsgs})

image

a little bit more code but nicely readable. Unless node.warn supports multiple arguments :wink: this is a good workaround for me ... more typing but ok. I have these all over the place normally commented out to use just in case

3 Likes

Thanks all for the great answers. I know use debug node to see output of one node. What I really want is debug the javascript code inside the "function" node. I will try what you guys suggested.

Thank!

Depending on how detailed you’d like to see the values, Node-RED actually exposes the NodeJS’ util module, which means you can use the util.inspect function from it:
https://nodejs.org/api/util.html

https://nodered.org/docs/user-guide/writing-functions#other-modules-and-functions

It has proven used up to me before when working with rather complex message structures. The result of the inspect call can be logged following the above responses.

As for actually debugging the code, I’m usually functioning as my own step-through debugger. With node.warn where needed to verify, and a pen/paper combination to keep track of the actual values.

1 Like

Hello

I love using util.inspect() in my node.warn instead to JSON.stringify() but since V3, Node-Red makes a warning message that util is not found, despite it is still working.

The only working recipe I found now is to add util module in the Setup tab of my function node but declaring it as ut or _util and using the same to call inspect

Is this the right way or is there a better way ?

Thanks