# Debugging function

**URL:** https://discourse.nodered.org/t/debugging-function/20831
**Category:** General
**Created:** [25 January 2020 03:51 UTC](https://discourse.nodered.org/t/debugging-function/20831 "2020-01-25T03:51:15Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![kanke](https://avatars.discourse-cdn.com/v4/letter/k/a698b9/32.png) [@kanke](https://discourse.nodered.org/u/kanke)
#### Post date: [25 January 2020 03:51 UTC](https://discourse.nodered.org/t/debugging-function/20831/1 "2020-01-25T03:51:15Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [25 January 2020 06:55 UTC](https://discourse.nodered.org/t/debugging-function/20831/2 "2020-01-25T06:55:17Z")

</div>

Please refer to the [documentation](https://nodered.org/docs/user-guide/writing-functions).

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

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [25 January 2020 10:09 UTC](https://discourse.nodered.org/t/debugging-function/20831/3 "2020-01-25T10:09:57Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [25 January 2020 11:21 UTC](https://discourse.nodered.org/t/debugging-function/20831/4 "2020-01-25T11:21:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [25 January 2020 14:20 UTC](https://discourse.nodered.org/t/debugging-function/20831/5 "2020-01-25T14:20:14Z")

</div>

> [@zenofmud](#):
>
> node.warn("your\_variable="+your\_variable);

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

```auto
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:

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

```

Example:

```auto
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6b4d4f66676bfef07e87550f978862670e9c255d.png)

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

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

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/a/7aa15b51d74df3a30cf658e072649a58dca39201.png)

a little bit more code but nicely readable. Unless node.warn supports multiple arguments 😉 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

---

<div class="post-metadata">

### Author: ![kanke](https://avatars.discourse-cdn.com/v4/letter/k/a698b9/32.png) [@kanke](https://discourse.nodered.org/u/kanke)
#### Post date: [25 January 2020 15:40 UTC](https://discourse.nodered.org/t/debugging-function/20831/6 "2020-01-25T15:40:23Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)
#### Post date: [25 January 2020 17:14 UTC](https://discourse.nodered.org/t/debugging-function/20831/7 "2020-01-25T17:14:34Z")

</div>

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://nodejs.org/api/util.html)

[https://nodered.org/docs/user-guide/writing-functions#other-modules-and-functions](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.

---

<div class="post-metadata">

### Author: ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)
#### Post date: [19 October 2024 10:03 UTC](https://discourse.nodered.org/t/debugging-function/20831/8 "2024-10-19T10:03:24Z")

</div>

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
