How to proper debug custom node

ok, then this is how i do NR development on full source...

Using a debug config

where starting debug is as simple as pressing F5

launch.json

{
    "version": "0.2.0",
    "configurations": [
        // add entry to launch.json to debug node-red
        {
            "type": "node",
            "request": "launch",
            "name": "debug node-red",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "env": { 
                "NODE_ENV": "development", 
                "port": "1881", 
            },
            "preLaunchTask": "npm: build-dev",
            "program": "${workspaceFolder}\\packages\\node_modules\\node-red\\red.js"
        }
    ]
}
You may also need to add a tasks.json

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "npm",
			"script": "build-dev",
			"group": "build",
			"problemMatcher": [],
			"label": "npm: build-dev",
			"detail": "build-dev"
		}
	]
}

Using the newer "Auto Attach" feature of VSCode

  1. Open a terminal window inside vscode
  2. set VSCode to use "Auto Attach: Always"
  3. run npm run dev
    Code_UFMjwrNzkq
4 Likes

That works :slight_smile:
The instance is running :slight_smile:

I now just need to work out how to actually use VSCode - I've tried many times over the years to get to grips with it but always failed and given up and gone back to using chalk on a blackboard :slight_smile:

[edit] Brilliant - I'm adding breakpoint and able to look at values - this should make things easier to see what's going on compared to sprinkling node.warns throughout the code:slight_smile:

image

I could really do with a dual monitor setup now though :slight_smile:

[edit2] Wow - I feel like a proper dev - I'm like Neo in The Matrix - I can see the code! :grinning:

3 Likes

Nice that you got the debugger working!
On the "sprinkling node.warns"... two tricks I use a lot:

  • console.log(foo) prints foo as a data structure, so you can see what's inside (node.warn requires a string, i.e., applies toString(), which is not as helpful)
  • try { ... } catch(err) { console.log(err.stack) } prints a stack trace if your code ... throws so you know exactly where the exception happened (this is also useful in function nodes)
1 Like