# Debug JS code inside function node

**URL:** <https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925>\
**Category:** FAQs\
**Created:** [19 May 2020 13:20 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925 "2020-05-19T13:20:02Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![tmichaeltx](https://avatars.discourse-cdn.com/v4/letter/t/73ab20/32.png) [@tmichaeltx](https://discourse.nodered.org/u/tmichaeltx)\
**Post date:** [19 May 2020 13:20 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/1 "2020-05-19T13:20:02Z")

</div>

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:

> [@Debug in Node-RED Blockly node](https://discourse.nodered.org/t/debug-in-node-red-blockly-node/5802/5):
>
> The Javascript code inside **function nodes** cannot be debugged, but if you create your own **custom node** then you can debug it (like you can debug all other Node-RED nodes). I have explained [here](https://discourse.nodered.org/t/how-to-debug-an-own-node-with-the-node-js-debugger/5737/4) how you can debug Node-RED using a Chrome debugger, in case you ever need it...

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.

---

<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:** [19 May 2020 13:22 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/2 "2020-05-19T13:22:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tmichaeltx](https://avatars.discourse-cdn.com/v4/letter/t/73ab20/32.png) [@tmichaeltx](https://discourse.nodered.org/u/tmichaeltx)\
**Post date:** [19 May 2020 13:27 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/3 "2020-05-19T13:27:28Z")

</div>

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?

---

<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:** [19 May 2020 13:34 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/4 "2020-05-19T13:34:33Z")

</div>

Yeah - double benefit.

I'll post a quick how to run up node-red in a second (gotta find the post I made - I refer back to it everytime i start out on a new machine)

---

<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:** [19 May 2020 13:38 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/5 "2020-05-19T13:38:37Z")

</div>

Here is it...

> [@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:…

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...

```javascript
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.

---

<div class="post-metadata">

**Author:** ![tmichaeltx](https://avatars.discourse-cdn.com/v4/letter/t/73ab20/32.png) [@tmichaeltx](https://discourse.nodered.org/u/tmichaeltx)\
**Post date:** [19 May 2020 13:44 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/6 "2020-05-19T13:44:07Z")

</div>

Thanks, for the quick responses.

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.

---

<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:** [19 May 2020 13:48 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/7 "2020-05-19T13:48:04Z")

</div>

Docker - that is a bit tougher.

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"

---

<div class="post-metadata">

**Author:** ![thinkbig1979](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thinkbig1979/32/22577_2.png) [@thinkbig1979](https://discourse.nodered.org/u/thinkbig1979)\
**Post date:** [19 May 2020 13:54 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/8 "2020-05-19T13:54:26Z")

</div>

The way I debug javascript for function nodes is by using [REPL.IT](https://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!

---

<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:** [19 May 2020 14:05 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/9 "2020-05-19T14:05:52Z")

</div>

I use REPL IT too (even posted some on this forum as proof of concepts)

I especially like it because it uses the monaco editor (same as VSCode) & gives you a full node environment.

However, you dont get node-red & debugging is mainly console.log

VSCode (or chrome developer tools) is the way to go (IMO)

---

<div class="post-metadata">

**Author:** ![thinkbig1979](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thinkbig1979/32/22577_2.png) [@thinkbig1979](https://discourse.nodered.org/u/thinkbig1979)\
**Post date:** [19 May 2020 14:24 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/10 "2020-05-19T14:24:58Z")

</div>

I will have to try the VS Code solution soon then!  
Just so you know though, [debugger works in repl.it as well.](https://repl.it/@mrhorwitz/Debugger) 🙂

---

<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:** [19 May 2020 14:30 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/11 "2020-05-19T14:30:07Z")

</div>

> [@thinkbig1979](#):
>
> Just so you know though, [debugger works in repl.it as well.](https://repl.it/@mrhorwitz/Debugger)

Of course it does - its client side (unlike a node-red function node that runs server side)

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [19 May 2020 15:39 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/12 "2020-05-19T15:39:51Z")

</div>

> [@Steve-Mcl](#):
>
> Just put `debugger` in your function node

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!

---

<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:** [19 May 2020 15:49 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/13 "2020-05-19T15:49:58Z")

</div>

> [@BartButenaers](#):
>
> Heuh can you debug a function node?

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.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [19 May 2020 21:21 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/14 "2020-05-19T21:21:40Z")

</div>

Hey Steve,  
Thanks a lot for the tip 👍 👍 👍 👍

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** _:

1. You need to configure _ **ONCE** _ the IP address of your Node-RED server, via `chrome://inspect`:

2. Enter a `debugger;` statement in your function node code:

3. Start your Node-RED server in debug-mode (which means it is allowed to attach a debugger process), e.g. on Linux:

4. Node-RED will start now, and NodeJs allows a debugger process to attach:

5. Now Chrome should see that the debugger can be attached:

6. _ **After** _ clicking the "_inspect_" link, a Chrome developer console window will open. You will arrive in the debugger at your function node:

7. 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...

8. 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 ...

Have fun with debugging your function node!  
Bart

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [19 May 2020 21:48 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/15 "2020-05-19T21:48:42Z")

</div>

I think we should move this to the FAQ category... really good stuff everyone ! Thanks

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [20 May 2020 05:32 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/16 "2020-05-20T05:32:05Z")

</div>

> [@dceejay](#):
>
> move this to the FAQ category

And how can I do that?  
I assume it should be via the "_ **wrench** _" button, but in all my posts there is only this option available:

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

---

<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:** [20 May 2020 06:37 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/17 "2020-05-20T06:37:32Z")

</div>

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)

---

<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:** [19 July 2020 06:37 UTC](https://discourse.nodered.org/t/debug-js-code-inside-function-node/26925/18 "2020-07-19T06:37:38Z")

</div>

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