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?
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.
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.
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.
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:
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:
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.
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)
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?
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.
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.