Debugging Nodes and Node-RED

2,746 views
Skip to first unread message

Julian Knight

unread,
Oct 15, 2017, 4:50:48 PM10/15/17
to Node-RED
Hi all, just a quite how-to:

I'm using Visual Studio Code from Microsoft to develop new nodes. VS Code is built on Node.JS as is Node-RED and it has built-in Node.js debugging.

The way I'm doing it is to open the ~/.node-red (userDir) folder, click on the debugging sidebar and click on the cog icon which opens a new file called launch.json in a folder called ./.vscode

Simply add the following configuration:

{
"version": "0.0.1",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"processId": "${command:PickProcess}"
}
}

Start up Node-RED then click on the green "start debugging" icon, you will get a popup to choose the active process you want to debug. All Node.js based processes will be listed.

Having chosen, the debugger may pause things, if so, click on the continue icon or press F5.

To debug your own (or someone elses) node, expand the "Loaded Scripts" list in the sidebar, find the appropriate script file, click on it to open. Now you can set breakpoints by clicking to the left of the line numbers.

When the execution hits a breakpoint, you will see the current call stack, variables and more.

I seriously wish that I'd got this working before because it is massively useful.

Zenofmud

unread,
Oct 15, 2017, 5:07:43 PM10/15/17
to node...@googlegroups.com
Thanks, I’ll have to try it. Up to now I've debug my functions by adding a debug. This is my example function showing how it works:

// add this to the top of your function node then any where you want 
// to see an item use: debug("01", "this is text to show")
var debugStatus = true;  // set this to 'false' to shut off the debug messages
function debug(debug_id, debug_value) {
    if (!debugStatus) {return;} // if debug is 
    var debugMsg = "debug-" + debug_id + ": " + JSON.stringify(debug_value);
    node.warn(debugMsg);
}

// the following are examples of how you can use it. I use unique numbers for the ID
// so I can locate them in the code, but you can use anything
debug(01,"This is a text string");
var requiredInputs = {"start": "start","stop": "stop"};
debug("inline",requiredInputs);
var xx = 33;
debug("in if", xx);
debug("value",34.7);
debug(2, "the temp is:" + 34.7);
debug(99, "msg.payload:" + msg.payload);
return null; 

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/390619e8-6e87-42dc-9c9c-6acbae2f137d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Simon H

unread,
Oct 15, 2017, 5:27:44 PM10/15/17
to Node-RED
must try it when I get a moment.  is VS Core a freebie for modjs dev?
Did you also try native Nodejs debugging in chrome?  It's also pretty powerful, but a bit of a pain to setup initially (I think it gets easier with later node versions).

Julian Knight

unread,
Oct 15, 2017, 5:31:48 PM10/15/17
to Node-RED
Hi Simon, yes VS Code is completely free (not the same product as Visual Studio).

The config for Chrome always put me off so I've never tried it. In fact, I've not really tried any previously - I was surprised at how easy it was to actually set up and start using. It is great to be able to see all the variables and expand the objects and arrays just like in the browser.

Julian Knight

unread,
Oct 15, 2017, 5:38:06 PM10/15/17
to Node-RED
The code in my uibuilder node is now too complex for me to always be able to rely on something like that. I'd already added Winston logger so as to be able to log to a separate file if a debug flag is set and I've probably hundreds of debug lines. Just when I start taking them out, I hit a problem and have to start putting them in again - so now they are there permanently but I use some flags to control the amount of output actually produced.

But when you aren't all that sure what is contained in some of the more complex objects and closures, nothing really beats a proper debugger.

Francois Vander Linden

unread,
Oct 16, 2017, 3:29:41 PM10/16/17
to Node-RED
Wow ! Thanks ! I'll try that ASAP !

skavan

unread,
Jan 27, 2018, 3:53:21 PM1/27/18
to Node-RED
Are you running vscode + node-red on an rpi? If so, where did you get the vscode binary?

Julian Knight

unread,
Jan 27, 2018, 5:34:50 PM1/27/18
to Node-RED
Nope. I think you can run vscode on Linux but I don't know if they have an ARM version for the Pi.

I do my development on Windows 10. I only install live(ish) code on the Pi. Normally from npm though occasionally from GitHub if I want to test something.

skavan

unread,
Feb 5, 2018, 10:59:04 AM2/5/18
to Node-RED
Got vscode running on the pi (stretch).
Used binaries from here: https://code.headmelted.com/.
Can't, for the life of me, get the attach to process to work....as it is always telling me "no entries to pick from".
Did you make any changes to node-red to enable this capability?

Julian Knight

unread,
Feb 5, 2018, 12:09:59 PM2/5/18
to Node-RED
No, it just worked. If you know what the process is, you should be able to provide the pid I think, can't quite remember.

Andy

unread,
Feb 5, 2018, 1:38:06 PM2/5/18
to Node-RED
I don't suppose you've tried this remotely?  Server on Pi, VS on laptop?

skavan

unread,
Feb 5, 2018, 9:42:52 PM2/5/18
to Node-RED
I'll try that next --- but I am unsure how to persuade a node-red node.js to expose debug functionality and on which port it does it!

p.s. As a related but different question, is there an easy and fast way to check (at runtime) the presence of the node-red environment.
i.e. I am happily writing a function in vscode and manually passing a payload:
if (environment === "vscode"){msg.payload = "my test payload"};
...rest of function...

right now, I comment lines in an out when I copy the code from vscode to node-red and have a little block:
var environment = "vscode"; // COMMENT OUT ONE OF THESE TWO LINES
//var environment = "node-red";

would be great if there was a way of setting "environment" by checking, well, the environment. any ideas on how the code can detect its running inside of node-red?

s.

Julian Knight

unread,
Feb 6, 2018, 6:46:59 AM2/6/18
to Node-RED
You would normally use an OS environment variable for that and then reference the var inside your node.js

If you look in your settings.js file for example, you should see that it uses `process.env.PORT`.

You can then change that from the command used to start node red:

PORT=9999 node-red

Environment variables can also be referenced directly as input to many node-red nodes.

skavan

unread,
Feb 14, 2018, 6:19:02 PM2/14/18
to Node-RED
K - switched to your way! installed node-red on windows, pointed vscode to user dir.

Seems to work -- but when I click on any scripts I get a "could not retrieve content." message in the file pane.

Also tried the derivative of your work, listed here: setup-a-own-node-red-dev-environment-in-5-min bu,t, this one just crashes on startup!

Am I just unlucky or dumb? Rhetorical question.

Julian Knight

unread,
Feb 14, 2018, 7:00:46 PM2/14/18
to Node-RED
I've not tried opening scripts other than those already loaded but perhaps they need to be installed locally rather than globally?

skavan

unread,
Feb 14, 2018, 7:06:27 PM2/14/18
to Node-RED
my bad - i wasn't clear. I'm referring to scripts that are already loaded!

s.

Mike Blackstock

unread,
Feb 17, 2018, 6:54:00 PM2/17/18
to Node-RED
Nice set up.  Never seen that 'PickProcess'.  cool.

I've always just launched Node-RED itself using vscode to debug node-red hacks and my nodes like this:

{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/red.js",
"runtimeArgs": [
"--preserve-symlinks"
]
}
]
}

Assumes you've cloned node-red from git and done the grunt build.  

The --preserve-symlinks was needed for me to make sure vscode followed symlinks to symlinked nodes that you're debugging.

RJSavant

unread,
May 4, 2018, 8:40:51 PM5/4/18
to Node-RED
This is super useful...
Thank you!
Reply all
Reply to author
Forward
0 new messages