# How to debug a function Node?

**URL:** <https://discourse.nodered.org/t/how-to-debug-a-function-node/38024>\
**Category:** Feature Requests\
**Created:** [26 December 2020 08:40 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024 "2020-12-26T08:40:50Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [26 December 2020 08:40 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024/1 "2020-12-26T08:40:50Z")

</div>

Continuing the discussion from [📋 Survey: logging to debug sidebar](https://discourse.nodered.org/t/survey-logging-to-debug-sidebar/21253):

As a (half-beginner) I've started to enhance a node, to make it work with multiple I2C buses.  
I'm writing my progress down to show how difficult is it for us, Newbies to get along with version 1.2.6  
_(Don't get me wrong, I love NR 💟 from the first screenshot I've seen it! Just some user-help improvements ℹ would boost a lot on nice experience, spare time and learning would be easier about it.)_

# 1. Examples from github

- Found code [here](https://github.com/fivdi/i2c-bus/blob/master/example/i2cfuncs.js),
- copied into a Function,
- spent a half hour to find out how fix the "required" error,
- than came the big surprice:  
NOTHING. No error, no warning, no info, no output on the sidebar.

# 2. global.log(), global.info(),

_After wathing 30+ YT videos and reading 1000 pages of documentation over the last 2 month my head is filled totally up to the top. Still, I remembered something like:_

- There was something about a `.warn('...')` function.
- code-extension does not work, so typing "global." shows no list of choices
- RED.warn() did not work either
- Somehow I remembered it should be used capitalized: ... so tried also ...WARN() variations.
- ... and many more trials.

# 3. No info/help on main page

Looking at : [logging...](https://nodered.org/docs/user-guide/runtime/logging) (Which is totally deepwater for a beginners anyway.)  
or at [error handling ...](https://nodered.org/docs/developing-flows/flow-structure#error-handling)  
or even at [cookbook error handling](https://cookbook.nodered.org/#error-handling)  
not a single hint is there.

One single line would have had helped a lot!

> You can use `node.debug('a='+a)` in a script inside a Function Node for sidebar-debugging.  
> See more here (link) ...

... At least the first link [logging...](https://nodered.org/docs/user-guide/runtime/logging) helped me to my next discovery:

# 4. Finding the console

 ![kép](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/872365929aab5e832eaa7e7f119fb3f925f485f5.png)

- So here I am, spent 1.5 hours to find out:
- `$ node-red-log` must be running from a console to view the log !!! 😬
- It hides usually behind the browers, so not too much help exept if you have a 2th monitor attached.
- 💡 IMHO there should be an alias too:`$ node-red-console` to keep naming consecvence.

So basically it wasn't me, not doing the job right, but the output went to a wrong place.

💡 One solution could be to place a button on the Debug Tab _with a counter_ ℹ to show "there is more behind". Something like this:

 ![kép](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/65c590e146b2d75840c91ea4a4bf58890393080b.png)

# 5. Forum search failures

[https://discourse.nodered.org/search?q=debug%20function](https://discourse.nodered.org/search?q=debug%20function)

My first search ended up at [this comment](https://discourse.nodered.org/t/node-debug-in-function-node-code/29825/2).

- 🙂 Great! Finally I know the KEYWORD IS: `node.XYZ()`
- (Still no auto-code-extension does, so typing "node." shows no list of choices. ☹ )
- So let's try: `node.info('Show Me!');`  
_(Why I've tried "info()", and not "error()" or "warn()" ?  
Because: I'd like to use warn() for problems. Not for simple infos.)_

The Result:  
NOTHING. NADA. Emptyness...

... many many other searches, no simple solution.  
That's how I've discovered the prev. topic.  
My oppinion:

- there should be MORE ways to do that, not just one. It's better to have 20 alias function for a thing as important than this than one hidden only.

# 6. SideBar help (Bug?)

Finally I remembered, each node has a built-in help.  
And YESSSS, Function has one too, with valuable information:

 ![kép](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/83842dd1688d9548c15c7157b404f43537aba250.png)

... So let's try it out:  
`node.log("My Logggg");`  
Aaaaaaand Result iiiiiiis:

 ![kép](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/e/cec37c6cea244eaee010cc274b3df2571a265ea5.png)

A big piece of nothing. As usual.  
That was the point I gave up and started this topic.

```auto
[{"id":"f3b2c9de.38ea7","type":"group","z":"34aad6f7.b85c6a","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["c7bd2502.8cab","fbdac8f7.f88548"],"x":74,"y":639,"w":372,"h":82},{"id":"c7bd2502.8cab","type":"inject","z":"34aad6f7.b85c6a","g":"f3b2c9de.38ea7","name":"Test Log","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":180,"y":680,"wires":[["fbdac8f7.f88548"]]},{"id":"fbdac8f7.f88548","type":"function","z":"34aad6f7.b85c6a","g":"f3b2c9de.38ea7","name":"test debug","func":"\n console.log('anything happening?');\n\nnode.log(\"My Logggg message\"); \n\n//node.warn('nodeW');\n//node.info('nodeI');\n//node.debug('nodeD');\n \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":350,"y":680,"wires":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 December 2020 08:51 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024/2 "2020-12-26T08:51:03Z")

</div>

> [@PizzaProgram](#):
>
> Forum search failures

If you really want to debug - use a debugger 🙂

Lots of clues on the forum

> [@How to proper debug custom node](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/13):
>
> This is how I debug a custom node in VS Code... It works for me however since this is a dist version of node-red its difficult sometimes to debug client side issues. Other than that, debug stop points work in my node js files. For debuging my .html files, i enter debugger into the function & open F12 (chrome debugger stops on the debugger statement) Setup... Install node-red as per instructions on [nodered.org](http://nodered.org) Run it one time so that .node-red folder is generated Install own node... cd c:…

> [@Debug JS code inside function node](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/5):
>
> Here is it... And although the topic is about debuging a custom node - it works perfectly well for your requirement... Just put debugger in your function node e.g... var x = msg.payload; debugger //...some complex code //...some complex code //...some complex code //...some complex code return msg; and if its all working, VSCode will pause on the debugger line.

> [@Dumb question about editing a node](https://discourse.nodered.org/t/dumb-question-about-editing-a-node/34434/63):
>
> add debugger key word into your node, restart node-red, open the browser & devtools, give it a go. Y As i said earlier, I've not used it but i suspect it will fire when deleted from the editor (before deploy - so in other words, still active in the runtime). If you want to remove callbacks at the server side, I would suggest you need to use this.on('close' and its parameters to determine if there is a clean up (at server side) to do. [reference](https://nodered.org/docs/creating-nodes/node-js#closing-the-node)

* * *

### demo of debugging a function node...

While running with vscode debugger attached, use the keyword `debugger` to instruct the debugger to stop...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/1/b163cfcc08984fc6de523943da627dc1d8db980c.png)

Real debugging...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/193b15c037848e6c3974047ebf774fc5800b62a0.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 December 2020 09:08 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024/3 "2020-12-26T09:08:00Z")

</div>

> [@PizzaProgram](#):
>
> # SideBar help (Bug?)
> 
> Finally I remembered, each node has a built-in help.  
> And YESSSS, Function has one too, with valuable information:  
> ![kép](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/83842dd1688d9548c15c7157b404f43537aba250.png)
> 
> ... So let's try it out:  
> `node.log("My Logggg");`  
> Aaaaaaand Result iiiiiiis:
> 
> ![kép](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/e/cec37c6cea244eaee010cc274b3df2571a265ea5.png)
> 
> A big piece of nothing. As usual.

It all depends what you are doing. for example, the title here is "debug a function node" - many of the answers to the questions you ask here can be found in [docs](https://nodered.org/docs) • [user guide](https://nodered.org/docs/user-guide) • [functions](https://nodered.org/docs/user-guide/writing-functions) ...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/4/24053a8db3c3b0194821c978e6f7a0304dbc13b2.png)

I am not saying that all the info is there but much of it is. It is not perfect but this is a huge program with minimal developers.

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [26 December 2020 09:27 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024/4 "2020-12-26T09:27:27Z")

</div>

Wow!

- Where did you copy that picture from? Link please!
- Is that sentence at the bottom marked with red by default? Or did you enhance it?
- Shouldn't be `node.debug("Variable", myVar);` also listed?

> [@Steve-Mcl](#):
>
> I am not saying that all the info is there but much of it is. It is not perfect but this is a huge program with minimal developers.

I totally agree with that!  
I know how hard it is for a deep-level / hardcore programmer to look "outside of his box" to see everthing from user-perspective.

Anyway, improving the code with +1 comment would make it much more self-explanatory:

```auto
    node.log("Log message"); // Warning: .log() is not visible at the editor's debug-sidebar only at the console.
    node.warn("Warning, both these variables are wrong:", myVar1, myVar2); 
    node.error(["Error " + msg.error.count, myVar1]);

```

Can you copy this code examples [here](https://nodered.org/docs/user-guide/runtime/logging) ?  
and to function node help?

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [26 December 2020 09:42 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024/5 "2020-12-26T09:42:09Z")

</div>

Also, as the [original topic](https://discourse.nodered.org/t/survey-logging-to-debug-sidebar/21253) recommended:  
Please enhance NR, so the:

# `console.log()` messages appear at the Debug sidebar too!

IMHO just because "only" 13% of us voted for this, doesn't necessary mean this possibility should be dropped.

And yes, IMHO `node.log()` should appear at the sidebar too!

- Why on earth is it disabled? Isn't that a BUG?

... maybe 3-4 tiny "filter buttons" would make it easier to reduce, if not needed:

[i] ❌ [w] ✅ [e] ✅ [d] ❌

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 December 2020 09:50 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024/6 "2020-12-26T09:50:30Z")

</div>

> [@PizzaProgram](#):
>
> Why on earth is it disabled? Isn't that a BUG?

It depends how much of the thread you link to you have read. I explain there why the debug sidebar doesn't show log/info level messages.

The Debug sidebar is _not_ intended to show the full log output of Node-RED. It is meant for debugging flows. This is why it doesn't show info level messages.

The log event handler used by the Debug sidebar is set to only display log events that are 'warn' level or higher.

The whole discussion in that other thread is about trying to resolve some of the inconsistencies and to provide a special case to the Function node to provide a way for it to more intuitively log to the Debug sidebar without having to log warn/error level messages - hence why we ended up with `node.debug(...)`.

We have an item on the backlog to provide better log level selection in the debug sidebar.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 December 2020 10:36 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024/7 "2020-12-26T10:36:38Z")

</div>

Your flow works perfectly for me. The `console.log()` output appears in syslog, `node.log()` displays in syslog and the node red log and `node.warn()` appears in syslog, node red log and the debug pane.

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [26 December 2020 10:47 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024/8 "2020-12-26T10:47:17Z")

</div>

> [@knolleary](#):
>
> why we ended up with `node.debug(...)` .

I agree that debug should go to debug console **only**.  
And the help about using `debug()` should be at advanced topics.

But If a beginner is copying an example to a Function node,  
or we do not want to use red coloured `error()` and `warn()` for showing simple informations,  
and sees that first line in the help `node.log()`, he will use it, just like I did.  
It it won't "work" like expected, because there is no indicator that would show any sign of a hidden message not visible where this trial happens.

> [@knolleary](#):
>
> It depends how much of the thread you link to you have read.

Sorry, you're right, only 70%. Red the half + the end.  
But I've simply tried all variants and it's not working the way a beginner would think.

@Colin  
node.log() does **not** show any message at the debug-sidebar for me.  
What version are you running?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 December 2020 12:26 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024/9 "2020-12-26T12:26:44Z")

</div>

> [@PizzaProgram](#):
>
> node.log() does **not** show any message at the debug-sidebar for me.

I didn't say it did.

---

<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:** [26 December 2020 13:27 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024/10 "2020-12-26T13:27:03Z")

</div>

Well the function documentation and help both provide information about logging.

However, I'd agree that node.xxx isn't terribly discoverable otherwise for beginners.

I wonder if something like a `RED.show(....)` function might be more obvious? Not sure that it would but putting it out there for thoughts. It would be just for the function node and would only output to the debug sidebar. Though perhaps you could have a 2nd param that also sent it to the actual log.

I think that having something that only outputs to the Editors debug sidebar would actually be rather helpful, the log already contains a lot of cruft sometimes.

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [26 December 2020 13:34 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024/11 "2020-12-26T13:34:44Z")

</div>

> [@TotallyInformation](#):
>
> something like a `RED.show(....)` function

IMHO we should stay with `node.xyz()` keyword. No need a new. one.  
`node.show();` sounds nice  
`node.info();` also

> [@TotallyInformation](#):
>
> could have a 2nd param

That could be confused with current function. ("text", MyObject)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 December 2020 13:42 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024/12 "2020-12-26T13:42:41Z")

</div>

At the very least read the previous thread to save having exactly the same discussion again.

The reason we went with adding `node.debug()` is because it sends its output to the **debug** sidebar.

We could discuss the API endlessly, but the fact remains the user has to know it exists to use it. So whatever it's called, it has to be documented. I can see we didn't update the docs to cover the changes to node.debug() following on from the previous discussion and changes that were made.

So rather than discuss endlessly over how it's spelt, it would be helpful to direct efforts to docs improvements.

---

<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:** [26 December 2020 14:16 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024/13 "2020-12-26T14:16:18Z")

</div>

I might be suffering from an excess of Christmas spirit(s) 🍹 However `node.debug` doesn't produce anything in the sidebar (it certainly exists as a property of `node`).

~~I can't find any documentation on it either - doubtless I'm being a bit thick today.~~

Right, Google let me down. I've found the docs now:

[Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#logging-events)

And sure enough:

> If there is no logger configured to capture those levels, they will not be seen.

So my takeway from that is that `node.debug` does not have any output by default? Surely that is not what is being asked for here? The ask was for a simple, discoverable way to output information to the debug sidebar that wasn't coloured for warn/error.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 December 2020 14:36 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024/14 "2020-12-26T14:36:23Z")

</div>

> [@TotallyInformation](#):
>
> So my takeway from that is that `node.debug` does not have any output by default? Surely that is not what is being asked for here? The ask was for a simple, discoverable way to output information to the debug sidebar that wasn't coloured for warn/error.

Which is precisely the topic of the thread linked to at the start where I polled the community for input on what the API should be to do this.

I just can't remember where we ended up in terms of implementing it.

Given I'm on holiday, I'm not digging in to it any deeper right now, but the plan will be for node.debug to log to the debug sidebar. I assumed I'd already implemented that, but I guess not.

---

<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:** [26 December 2020 15:21 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024/15 "2020-12-26T15:21:48Z")

</div>

Sorry to disturb your holiday. For when you return,

The current implementation of node.debug in a function node appears to be:

```auto
function() {
    node.debug.apply(node, arguments);
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 February 2021 15:21 UTC](https://discourse.nodered.org/t/how-to-debug-a-function-node/38024/16 "2021-02-24T15:21:49Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
