# Add Docs on VS Code to debug Node-RED and nodes

**URL:** https://discourse.nodered.org/t/add-docs-on-vs-code-to-debug-node-red-and-nodes/11891
**Category:** Developing Nodes
**Created:** [5 June 2019 21:14 UTC](https://discourse.nodered.org/t/add-docs-on-vs-code-to-debug-node-red-and-nodes/11891 "2019-06-05T21:14:16Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![tree-frog](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tree-frog/32/89303_2.png) [@tree-frog](https://discourse.nodered.org/u/tree-frog)
#### Post date: [5 June 2019 21:14 UTC](https://discourse.nodered.org/t/add-docs-on-vs-code-to-debug-node-red-and-nodes/11891/1 "2019-06-05T21:14:17Z")

</div>

For those who are beginning Node-RED and running into some problems with particular nodes, Can someone please post a typical configuration in order to run Node-RED in VS Code in order to facilitate finding problems within the nodes? Things to do, Things NOT to do, etc... This would be helpful if someone that uses VS Code regularly and has tips for using the debugger. Not everyone is a JS programmer out of the gate... Perhaps if someone could point to a resource for reading that is already published that gets you set up and ready to run and debug a simple flow?

Thanks in advance.

---

<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: [6 June 2019 00:08 UTC](https://discourse.nodered.org/t/add-docs-on-vs-code-to-debug-node-red-and-nodes/11891/2 "2019-06-06T00:08:37Z")

</div>

I've a blog post that touches on this:

> **[Node-RED - How to use the Visual Studio Code debugger](https://it.knightnet.org.uk/kb/nr-qa/vscode-debugger/)**
>
> Using the Visual Studio Code debugger to debug Node-RED issues

Though I've just noticed an error on that page that I need to fix - it was translated from a previous web host and hasn't translated the code block properly.

There really isn't much to it. I keep a couple of VScode workspaces, one for Node-RED itself (e.g. based on the userDir folder on my development PC) and one based around the folder of the node I'm developing. I typically have both open and run Node-RED from the terminal session of the userDir workspace.

You can run Node-RED using Node.js's `--inspect` option which enables debugging. You can use the VScode debugger - the easiest way is to attach the debugger to the current Node-RED process. To help with that, I've amended my Node-RED `settings.js` file to display the PID for the Node-RED process at startup. Add the following before the `module.exports`. You can simplify the code if you don't need to write the PID to a file.

```javascript
var fs = require('fs') // The `https` setting requires the `fs` module.
//var _ = require('lodash'); // lodash (improved underscore) for JS utility fns

/** Save a PID file so that Node-RED can be easily restarted even when run manually */
const pid = process.pid
console.info('PID: ', pid)
fs.readdir('.', (err, files)=>{
    if (err) throw err

    for (var i = 0, len = files.length; i < len; i++) {
        var match = files[i].match(/.*\.pid/)
        if(match !== null) fs.unlink(match[0], (err) => {
            if (err) throw err
        })
    }

    fs.writeFile(`${pid}.pid`, pid, (err) => {
        if (err) throw err
    })
})

```

It is also possible to configure VScode to start Node-RED with the appropriate settings but I generally don't bother.

You can also use Chromium's developer tools with Node-RED once you're using the `--inspect` option. I use Vivaldi and it adds a button to the dev tools interface if something is running with inspect turned on.

When using the debugging tools, just note that, if you don't start the debugger quickly enough, you don't appear to get a lot of data because Node-RED has already done all of its setup. It is all there but if you need to debug the startup of a custom node, you need to start the debugger early enough to capture what is happening when Node-RED loads your node's module.

---

<div class="post-metadata">

### Author: ![tree-frog](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tree-frog/32/89303_2.png) [@tree-frog](https://discourse.nodered.org/u/tree-frog)
#### Post date: [6 June 2019 15:01 UTC](https://discourse.nodered.org/t/add-docs-on-vs-code-to-debug-node-red-and-nodes/11891/3 "2019-06-06T15:01:33Z")

</div>

Mr. TotallyInformation,  
Wow, That's awesome. My jaw just dropped. (Literally).

Thanks!

---

<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: [6 June 2019 15:05 UTC](https://discourse.nodered.org/t/add-docs-on-vs-code-to-debug-node-red-and-nodes/11891/4 "2019-06-06T15:05:54Z")

</div>

Well, just have a go - it takes some time to get your head around exactly what is going on and what everything means so you will need to invest some learning time.

I don't use it often but sometimes it is the only way to see what is going on. Especially if you need to track down things inside the Node-RED core or Dashboard.

---

<div class="post-metadata">

### Author: ![tree-frog](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tree-frog/32/89303_2.png) [@tree-frog](https://discourse.nodered.org/u/tree-frog)
#### Post date: [6 June 2019 15:19 UTC](https://discourse.nodered.org/t/add-docs-on-vs-code-to-debug-node-red-and-nodes/11891/5 "2019-06-06T15:19:19Z")

</div>

I'm trying to take a stab at debugging this issue with ES6 cache behavior and mssql with connections to different databases. Every time I run a flow to update the data (after it ran successfully once already) it fails and reports that a table doesn't exist in the database I'm not trying to read from! Apparently connections that already are defined are not "new", so the cache is assumed to be valid, thus not updated for the database your trying to read from... It used to work fine for older versions of Node.js.

---

<div class="post-metadata">

### Author: ![Thepadol](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Thepadol](https://discourse.nodered.org/u/Thepadol)
#### Post date: [16 March 2020 12:20 UTC](https://discourse.nodered.org/t/add-docs-on-vs-code-to-debug-node-red-and-nodes/11891/6 "2020-03-16T12:20:01Z")

</div>

For VS code to debug a node I'm writing for NR, what does "starting the debugger quickly enough.." mean? In my case I'm starting with a very simple node to make sure I have the env setup correctly. It's a custom node that requires a triggered injection to generate an output. So I don't really care if NR is initialized...has to be. But then it should be there waiting till I activate the injection and at that point it should stop at the breakpoint I want in my node. Doesn't seem to do this. Either I am starting NR with missing/wrong parameters or VS code is looking elsewhere.

---

<div class="post-metadata">

### Author: ![tree-frog](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tree-frog/32/89303_2.png) [@tree-frog](https://discourse.nodered.org/u/tree-frog)
#### Post date: [16 March 2020 14:41 UTC](https://discourse.nodered.org/t/add-docs-on-vs-code-to-debug-node-red-and-nodes/11891/7 "2020-03-16T14:41:41Z")

</div>

I believe it refers to "Chromium development tools"...

---

<div class="post-metadata">

### Author: ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)
#### Post date: [23 February 2021 21:18 UTC](https://discourse.nodered.org/t/add-docs-on-vs-code-to-debug-node-red-and-nodes/11891/8 "2021-02-23T21:18:02Z")

</div>

I followed the description and it seems to work, but I didn't get my script. Whats wrong? Does I nee to export it to a .js into the working dir?

- I started the node-red from working dir ..node-red. There I can see the flow "flow\_pcName.json".
- Than I opened this Dir in VSC
- started Debugging with Atach process (--\> in console window I see:

```auto
Debugger listening on ws://127.0.0.1:9229/f00dc4e2-53bd-4826-8486-54fbc8a1b428
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.

```

In "Loaded scripts" I get only:  
 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/c/ecd056b290c106ca29f5cf5e648b234cd3ebb15e.png)

but there I cannot find my test function node. I also tried to find it with the command Ctrl+Shift+F, but didn't find the function node aVSTestFunction.

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/9/b96905f8f942de0c4692e2ab25337b04602c2dd2.png)

I hope someone can help me on the last steps

---

<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: [23 February 2021 21:28 UTC](https://discourse.nodered.org/t/add-docs-on-vs-code-to-debug-node-red-and-nodes/11891/9 "2021-02-23T21:28:42Z")

</div>

This works for me every time...

> [@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 to debug your function node code, simply enter the key word `debugger` in the function code on a separate line (usually at the top of the code), deploy the code. Then fire the function (via an inject or whatever)

---

<div class="post-metadata">

### Author: ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)
#### Post date: [24 February 2021 12:20 UTC](https://discourse.nodered.org/t/add-docs-on-vs-code-to-debug-node-red-and-nodes/11891/10 "2021-02-24T12:20:31Z")

</div>

Many, many thanks @Steve-Mcl🙂🙂

This will be a great step ahead in my developments. It's not like developing in a real IDE like JAVE,C#,.... because it is not bidirectional (e.g. change a variable and go ahead, or edit source), but it helps a lot to test, understand and find errors.

Or is there perhaps also a trick to use this editor to write a function for node red? I mean to make the file/script writable? So I can edit in VS code and have it than in node red also?

---

<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: [24 February 2021 12:33 UTC](https://discourse.nodered.org/t/add-docs-on-vs-code-to-debug-node-red-and-nodes/11891/11 "2021-02-24T12:33:07Z")

</div>

No, you must edit the code in node-red then when the function runs you get the debugger stoping on your new code. What you are asking is not currently possible (and likely never will be as it'd like need a custom debugger). What you get is the dynamic, unchangeable code. But it most definitely helps to step through and inspect variables to understand what's going on in your code.

---

<div class="post-metadata">

### Author: ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)
#### Post date: [24 February 2021 12:58 UTC](https://discourse.nodered.org/t/add-docs-on-vs-code-to-debug-node-red-and-nodes/11891/12 "2021-02-24T12:58:46Z")

</div>

Yyes, it helps a lot and I'm really happy that it works 🙂

---

<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: [24 February 2021 18:10 UTC](https://discourse.nodered.org/t/add-docs-on-vs-code-to-debug-node-red-and-nodes/11891/13 "2021-02-24T18:10:59Z")

</div>

Custom debugging is, I believe, on the roadmap 🙂

---

<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: [24 February 2021 18:17 UTC](https://discourse.nodered.org/t/add-docs-on-vs-code-to-debug-node-red-and-nodes/11891/14 "2021-02-24T18:17:34Z")

</div>

Yeah, I've seen and read the odd thing about improvements around debugging but I didn't interpret any of that as a custom vscode debugger that would permit edits from ide to node-red. That would be wonderful but I think it's a stretch.
