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

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.

I've a blog post that touches on this:

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.

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.

1 Like

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

Thanks!

1 Like

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.

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.