# How to debug an own node with the node.js debugger?

**URL:** https://discourse.nodered.org/t/how-to-debug-an-own-node-with-the-node-js-debugger/5737
**Category:** Developing Nodes
**Created:** [12 December 2018 23:12 UTC](https://discourse.nodered.org/t/how-to-debug-an-own-node-with-the-node-js-debugger/5737 "2018-12-12T23:12:11Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![noir](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@noir](https://discourse.nodered.org/u/noir)
#### Post date: [12 December 2018 23:12 UTC](https://discourse.nodered.org/t/how-to-debug-an-own-node-with-the-node-js-debugger/5737/1 "2018-12-12T23:12:11Z")

</div>

I'm creating an own node but I'm stuck with it so I want to find the error with the node.js debugger. However, setting breakpoints in **any** node seems not to work at all.

Forgive me when this looks a bit hacky to you - I'm not very experienced with JS development. (yet)

I installed Node-RED via requiring it in the `package.json` in a empty folder and running `npm install`.

From there I go into `node_modules/node-red` and run it with `node ./red.js`. This is working so far. It also works to install another node by running `npm install node-red-<pkg>` in the `~/.node-red` directory.

For debugging, I run node.js like this `node --inspect-brk ./red.js`. After that, I connect to the node.js debugging server with Chromium and I see it working so far. I can step through files of the Node-RED core.

The problem starts with inspecting a node which is installed in node\_modules via `npm link`. I actually tried it the hard way by inserting a `debugger;` statement as the first line of a node constructor like this:

```
function StreamdeckInNode(n) {
    debugger;
    RED.nodes.createNode(this, n);
[...]
}
  RED.nodes.registerType("streamdeck-in", StreamdeckInNode);

```

But the debugger doesn't stop at this point. I even tried to modify an existing and working node directly in the `node_modules` folder like above but still no breakpoint.

I have no idea what is going on here.

My environment (copied from the Node-RED output):

Node-RED version: v0.19.4  
Node.js version: v8.11.3  
Linux 4.19.6

I run node.js 8 on purpose (via NVM) because a certain node.js module doesn't work with 10+.

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [13 December 2018 07:17 UTC](https://discourse.nodered.org/t/how-to-debug-an-own-node-with-the-node-js-debugger/5737/2 "2018-12-13T07:17:30Z")

</div>

Hey @Noir,

That is very weird. I alway use Chrome to debug all my contributions to Node-RED. Have had a lot of issues in the past, but not the one that you have...

Why are you **only** using `--inspect-brk` ? Because that is used to debug stuff at the start of your Node-RED application (like you can step through Node-RED). I only use that if I need to debug some flow startup behaviour. You should normally add `--inspect=0.0.0.0:5858`, so Chrome can connect to your NodeJS application to communicate via the debugger protocol.

It normally works fine, but it still stays experimental technologie! So if you upgrade to later NodeJs versions, you will see that the way of working changes unfortunately.

Good luck !  
Bart

---

<div class="post-metadata">

### Author: ![noir](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@noir](https://discourse.nodered.org/u/noir)
#### Post date: [13 December 2018 11:54 UTC](https://discourse.nodered.org/t/how-to-debug-an-own-node-with-the-node-js-debugger/5737/3 "2018-12-13T11:54:39Z")

</div>

> [@BartButenaers](#):
>
> Why are you **only** using `--inspect-brk` ?

I want to make make sure that nothing interesting is executed before the debugger is attached.

Could you describe how your development environment and your workflow looks like? It would be very useful to me as a reference.

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [13 December 2018 12:39 UTC](https://discourse.nodered.org/t/how-to-debug-an-own-node-with-the-node-js-debugger/5737/4 "2018-12-13T12:39:31Z")

</div>

Hey @noir

You will need to use both command arguments to let Node-RED halt at the first line **AND** make sure that from then on your debugger can communicate with NodeJs:

```auto
node --inspect --debug-brk /usr/lib/node_modules/node-red/red.js

```

Will add some extra explanation below, for those who want to debug their code using Chrome...

Some remarks:

- Note that you **cannot** debug code in function nodes! The code in a function node is one single piece of text, that is handed over to NodeJs to execute as one whole. I wrote some extra [explanation](https://groups.google.com/d/msg/node-red/LxgNFTGMjiE/hrlpbzBzEgAJ) in the past about it ...
- Probably Visual Studio Code is much more powerful, but I debug using Chrome because it is simple and offers everything I need ...
- When you search on the web, you will also come across a **node-inspector** module. In the beginning NodeJs had a dedicated debug protocol, so you needed that _browser plugin_ to connect your browser debugger to NodeJs. However now NodeJs supports a standard debug protocol, so browser like Chrome can directly communicate with NodeJs without plugins. So we won't use that technology in this discussion ...

I use Chrome both to debug:

- The **client-side** of my node (which runs in my browser like the Dashboard or the Flow editor), i.e. the Javascript used for my node's config screen. For example stuff from the `oneditprepare` function.
- The **server-side** of my node (which runs in my flow on my Raspberry), i.e. the code that is e.g. triggered when a new input message arrives.

As mentioned before the technology (to debug server-side NodeJs from Chrome) is **experimental** , so it changes from time to time how it has to be used:

- In **older NodeJs versions** you had to startup Node-RED (on a Raspberry for example) like this:

- Starting from starting from **NodeJs version 7.5.0** , you need to specify **extra** in your command which remote ip address is allowed to debug (see this security [issue](https://github.com/nodejs/node/issues/11591) in NodeJs)):

- Starting **from NodeJs version 8.?** , the command displays a `ws://...` address instead of `chrome-devtools://...` address. To open such an URL with Chrome you have to follow these steps:

I'm only using the latest mechanism, so I cannot test the others anymore for you (in case of troubles)!

Bart

---

<div class="post-metadata">

### Author: ![noir](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@noir](https://discourse.nodered.org/u/noir)
#### Post date: [18 December 2018 02:45 UTC](https://discourse.nodered.org/t/how-to-debug-an-own-node-with-the-node-js-debugger/5737/5 "2018-12-18T02:45:25Z")

</div>

Thank you very much for your in-depth instructions. I decided to grab a Raspberry PI with Raspbian as a clean environment (This was my long term target platform anyway) to start over and deployed my nodes on it while keeping your instructions in mind. It is working now! Seems like I really messed up my local environment somehow.

---

<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: [23 December 2018 02:04 UTC](https://discourse.nodered.org/t/how-to-debug-an-own-node-with-the-node-js-debugger/5737/6 "2018-12-23T02:04:49Z")

</div>

Not sure if this works for chrome debugging but in vscode, I include `--preserve-symlinks` in the debug settings file.

Not able to paste a copy of my launch JSON right now but if you do a quick google search on _vscode launch node -preserve-symlinks_ you'll find the info.

Hope that helps you or others debugging sym linked own nodes.

---

<div class="post-metadata">

### Author: ![nileio](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nileio/32/9444_2.png) [@nileio](https://discourse.nodered.org/u/nileio)
#### Post date: [25 July 2019 11:59 UTC](https://discourse.nodered.org/t/how-to-debug-an-own-node-with-the-node-js-debugger/5737/7 "2019-07-25T11:59:58Z")

</div>

@BartButenaers @noir please note the `--inspect --debug-brk` has been deprecated as of Node 7.7.0 .. recommendation is now to use

> --inspect-brk

[https://nodejs.org/de/docs/guides/debugging-getting-started/#legacy-debugger](https://nodejs.org/de/docs/guides/debugging-getting-started/#legacy-debugger)

---

<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: [22 August 2019 13:54 UTC](https://discourse.nodered.org/t/how-to-debug-an-own-node-with-the-node-js-debugger/5737/8 "2019-08-22T13:54:43Z")

</div>

I never did get back to this (until now)...

> [@How to proper debug custom node](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/13):
>
> This is how I debug a custom node in VS Code... It works for me however since this is a dist version of node-red its difficult sometimes to debug client side issues. Other than that, debug stop points work in my node js files. For debuging my .html files, i enter debugger into the function & open F12 (chrome debugger stops on the debugger statement) Setup... Install node-red as per instructions on [nodered.org](http://nodered.org) Run it one time so that .node-red folder is generated Install own node... cd c:…
