# How to proper debug custom node

**URL:** <https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308>\
**Category:** General\
**Created:** [22 March 2019 10:07 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308 "2019-03-22T10:07:52Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![mathurv](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@mathurv](https://discourse.nodered.org/u/mathurv)\
**Post date:** [21 August 2020 17:05 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/22 "2020-08-21T17:05:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 August 2020 19:40 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/23 "2020-08-21T19:40:21Z")

</div>

> [@mathurv](#):
>
> how do I debug the .html file

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)

---

<div class="post-metadata">

**Author:** ![mathurv](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@mathurv](https://discourse.nodered.org/u/mathurv)\
**Post date:** [22 August 2020 00:20 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/24 "2020-08-22T00:20:33Z")

</div>

Thank you. It is working.

---

<div class="post-metadata">

**Author:** ![kakyoism](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kakyoism/32/46283_2.png) [@kakyoism](https://discourse.nodered.org/u/kakyoism)\
**Post date:** [13 September 2021 04:12 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/25 "2021-09-13T04:12:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kakyoism](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kakyoism/32/46283_2.png) [@kakyoism](https://discourse.nodered.org/u/kakyoism)\
**Post date:** [13 September 2021 04:20 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/26 "2021-09-13T04:20:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [23 November 2021 11:54 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/27 "2021-11-23T11:54:12Z")

</div>

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

# Where do I find the correct red.js file?

 ![kép](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/a/7a2d5646af803fc0a9ddb1151477db27ade79086.png)

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [23 November 2021 14:57 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/28 "2021-11-23T14:57:07Z")

</div>

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:

```auto
  "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:

```auto
        {
            "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",
        }

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 November 2021 16:25 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/29 "2021-11-23T16:25:33Z")

</div>

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 😀

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

```auto
    "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)](https://nodejs.org/docs/latest-v16.x/api/debugger.html)

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [23 November 2021 19:43 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/30 "2021-11-23T19:43:33Z")

</div>

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 ?

```auto
  "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"
}

```

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [23 November 2021 19:52 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/31 "2021-11-23T19:52:28Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 November 2021 20:41 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/32 "2021-11-23T20:41:39Z")

</div>

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/`.

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [27 November 2021 13:19 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/33 "2021-11-27T13:19:34Z")

</div>

Still not stopping at any breakpoint ☹

- 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.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 November 2021 11:50 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/34 "2021-11-28T11:50:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [21 February 2023 08:21 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/36 "2023-02-21T08:21:02Z")

</div>

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 )

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/5/95a3d6fa7d6ce533edfbf205d0b27d2fb8fa5be4.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 February 2023 08:37 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/37 "2023-02-21T08:37:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [21 February 2023 08:51 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/38 "2023-02-21T08:51:24Z")

</div>

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! 🙂

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 February 2023 08:53 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/39 "2023-02-21T08:53:12Z")

</div>

press F5

alternatively, the "play" button is here

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/7/979a05b3260eaebd270d560a7dd6ef3ca90a3851.png)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [21 February 2023 09:03 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/40 "2023-02-21T09:03:31Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/b/9b3c499e9192d47f4d55444f63327f2e4f50066b.png)

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 February 2023 09:11 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/41 "2023-02-21T09:11:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [21 February 2023 09:29 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/42 "2023-02-21T09:29:11Z")

</div>

> [@cymplecy](#):
>
> I am attempting to do this on a full dev source instance of Node-RED

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

[Previous page](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308.md?page=1)

[Next page](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308.md?page=3)
