How to proper debug custom node

Hi Steve,
Thanks for the quick response.
I tried on the sample "lower-case" node and it is working on the "lower-case.js" file. But.. how do I debug the .html file AngularJS code? and in case if I am calling Angular code written in another .js file (sitting in the same directory), how do I debug that? browser debugger is not helping much.
Please help

The html is client side so you can't debug in the same way.

Add the keyword debugger in your code where you want to stop & open chrome Devtools (F12)

1 Like

Thank you. It is working.

Thanks for the excellent guide!

I've got to the point where my breakpoints work.
However, my node's JS code relies on external programs which I locate using relative paths, relative to the actual root folder of the node implementation. This is where the VSCode debugger falls apart:

  1. AFAIK, the custom nodes are installed as symlinks into ~/.node-red
  2. My node's JS code uses _dirname as the anchor folder to find external resources.
  3. Looks like VSCode debugger interprets the symlinked folder as a physical folder under ~/.node-red, which breaks all my relative paths.

The code runs find in the real node-red environment.
Is there any best practice to make VSCode debugger work with the symlink'ed node without breaking the production code that works fine with the existing relative paths?

Alright, solved it myself. All I need is to use fs.realpath() around my _dirname usage.

1 Like

... Trying to make this work this since a week under Raspberry Pi linux.

Where do I find the correct red.js file?

It simply does not stop at any breakpoint.
If I start VS Code and hover my mouse over the breakpoint list > they all show:
"Unbound Breakpoint"

This is my package.json file contains this:

  "scripts": {
    "start": "node node_modules/node-red/red.js -v -u . -s settings.js",
    "debug": "node --nolazy --inspect-brk=5585 node_modules/node-red/red.js -v -u . -s settings.js"
  },

And the launch.json:

        {
            "type": "node",
            "name": "Run Script: debug MCP2",
            "program": "/usr/lib/node_modules/node-red/red.js",
            "cwd": "/home/pi/.node-red/node_modules/mcp-pfc-aio",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "runtimeArgs": ["--preserve-symlinks", "--experimental-modules"],
            "request": "launch"
            //"command": "npm run debug",
        }

Put a debugger statement early in your settings.js file (outside the module.exports) to trigger a break when running under the inspector. Then you can control the progress through the startup and execution. Otherwise, as you've discovered, it is very hard to get Node-RED to stop for a second :grinning:

I use my alternate installer and so run a local copy of Node-RED. To run under the debugger, I have this npm script:

    "inspect": "node --inspect node_modules/node-red/red.js --userDir ./data"

In the package.json file in the master folder. I leave the debugger line in settings.js because it is ignored if you run node-red without --inspect.

Debugger | Node.js v16.13.0 Documentation (nodejs.org)

1 Like

Thanks for the debugger tip, I've inserted to both the settings.js and my app's .js file too.

I don't really understand where to put this "inspect": ... thing inside launch.json ?

  "repository": {
    "type": "git",
    "url": "https://github.com/pizzaprogram/mcp23017-pfc8574-aio"
  },
  "scripts": {
    "inspect": "node --inspect node_modules/node-red/red.js --userDir ./data",
    "start": "node node_modules/node-red/red.js -v -u . -s settings.js",
    "debug": "node --nolazy --inspect node_modules/node-red/red.js -v -u . -s settings.js"
  },
"version": "2.1.0"
}

If I start NR from VS Code, I get thi in the terminal window:

npm run debug
pi@OH:~/MCP23017-PFC8574-AIO $ npm run debug

> mcp-pfc-aio@2.1.0 debug
> node --nolazy --inspect node_modules/node-red/red.js -v -u . -s settings.js

Debugger listening on ws://127.0.0.1:9229/18c4fb3d-4a1d-4bf2-9b82-4740ab7499a6
For help, see: https://nodejs.org/en/docs/inspector
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module '/home/pi/MCP23017-PFC8574-AIO/node_modules/node-red/red.js'

Somehow it is mixing the NR path with my APP's path.

That's because you missed the part where I said I was using my alternate installer.

That puts the node-red installation into a master folder with the userDir in the ./data sub-folder. So if using the package.json file in the master folder, lets say called ~/redness/, then node-red is actually in the folder ~/redness/node_modules/node-red/red.js.

Because the package.json file itself is ~/redness/package.json, the relative location of the red.js file is node_modules/node-red/red.js (no leading / which is what makes it relative. For clarity, it would have been better for me to write ./node_modules/node-red/red.js which is the same relative folder but perhaps a bit clearer to read.

If you installed Node-RED the "usual" way, as a global install, then red.js is somewhere completely different. Try running whereis red.js from the terminal, that should tell you where it actually is.

In addition, you can miss off the --userDir ./data part if you are simply using the default userDir location of ~/.node-red/.

1 Like

Still not stopping at any breakpoint :frowning:

  • What OS are you running NR + VSCode on?
  • What is showing whereis red.js for you?
    For me: red: /usr/bin/red /usr/share/man/man1/red.1.gz

But in that folder there is no red.js file, only a red command script.

OS shouldn't matter. I'm assuming you are on Linux. I generally do development on Windows just because it is my daily driver. But I run live on Linux.

/usr/bin/red/red.js is the location you want then. If there is only a command script, look in that and it should say where the .js file is.

Just trying to setup a debugging environment (FYI I know nothing about using VSCode)

I am attempting to do this on a full dev source instance of Node-RED and when I reach the launch.json part - I don't have a launch.json

And clues on how to proceed (this instance is running in d:\node-red )

Things have changed slightly. You need to create one first. On the left hand side bar icons, you will see like a play button (debug) usually the 4th down from the top. Select that and then in the left hand side baryou will see a button saying something like create a config. That will generate the launch file.

Alternatively, it is now possible to simply start by running a command line in the built-in console, you just first enable smart debug.

Sorry but this is all of the top of my head and I don't have time to screenshot and show you exact steps ATM Simon. So try and figure it out & let me know if you succeed or not, should be able to help more in about 1 hours time.

1 Like

No rush - got the launch.json created but don't know what to press next to actually launch NR

Will keep looking and pressing random buttons! :slight_smile:

press F5

alternatively, the "play" button is here

image

That launched it but got error - I think that this line that I put in launch.json isn't right

Like I said this a source code dev instance in D:\node-red

image

I've found a few "red.js" in a few sub-folders but don't know which is the right one

ah, just a second - are you developing a custom node or modifying core stuff?

This thread is primarily meant for a user firing up a standard (npm i -g) node-red install to debug a node they are developing.

Sorry - I did own up at the beginning
(This is all to help me with my switch node FR stuff)