# 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:** 1

<div class="post-metadata">

**Author:** ![Deca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/deca/32/6515_2.png) [@Deca](https://discourse.nodered.org/u/Deca)\
**Post date:** [22 March 2019 10:07 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/1 "2019-03-22T10:07:52Z")

</div>

Hi,

I've created my fist custom node following the ufficial doc and so far it's fine, but is not really clear to me how to debug efficiently the node...everytime I modify something and want to test it out I need to re-install the node using `npm install`?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 March 2019 10:21 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/2 "2019-03-22T10:21:30Z")

</div>

Hi @Deca

if you followed the guide, you should have run `npm install <location of node module>` which, if you have npm 5 or later, will have created a filesystem link between the `.node-red` directory and where your node is located. That means you don't need to reinstall the node each time - you just need to restart node-red.

Personally, I use [`nodemon`](https://nodemon.io/) to run node-red and have it automatically restart whenever it sees changes to the node files.

---

<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:** [22 March 2019 10:34 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/3 "2019-03-22T10:34:54Z")

</div>

> [@knolleary](#):
>
> Personally, I use [`nodemon`](https://nodemon.io/) to run node-red and have it automatically restart whenever it sees changes to the node files.

I now have a very strong muscle memory: ctrl-c, uparrow, enter (selects "y" to cancel the script), uparrow, enter (selects the last command which starts Node-RED) 🙂

I often don't use a watcher because of another strong muscle-memory: ctrl-s !

As I develop using VScode, I have an npm start script to start the dev version of Node-RED manually. I also have a similar "inspect" script that starts Node-RED with Node.JS's inspect feature turned on so that I can dig into detailed debugging if needed. The Vivaldi browser (and I imagine all other Chromium-based browsers) has a handy icon in the developer tools interface that opens a Node.js debugging window. VSCode of course, also has this built in.

---

<div class="post-metadata">

**Author:** ![Deca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/deca/32/6515_2.png) [@Deca](https://discourse.nodered.org/u/Deca)\
**Post date:** [26 March 2019 08:50 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/5 "2019-03-26T08:50:54Z")

</div>

Sorry for the late reply, weekend arrived and then I had other urgent work to do...now I'm back to testing my custom node 🙂  
I'm working on windows so I guess there no filesystem link generation, at the moment I was editing the node directly in the /node\_modules directory...anyway I'll give a try to nodemon, because manually relaunching node-red every time I touch something is very exausting and timeconsuming

---

<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:** [26 March 2019 13:06 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/6 "2019-03-26T13:06:41Z")

</div>

> [@Deca](#):
>
> 'm working on windows so I guess there no filesystem link generation

FS Links work fine on Windows and have done ever since NTFS was introduced.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [26 March 2019 13:34 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/7 "2019-03-26T13:34:10Z")

</div>

Just use `npm link` in your custom node's devel directory to create a global link.  
Then use `npm link node-red-contrib-mynode` in your NR workspace dir to "install" the node as a symbolic link. Substitute the package name, of course. 😀

Works fine on Windows, too.

---

<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:** [26 March 2019 23:33 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/8 "2019-03-26T23:33:03Z")

</div>

> [@kuema](#):
>
> Works fine on Windows, too.

I find it a lot easier and simpler to just `npm install /path/to/source/code`

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [27 March 2019 06:17 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/9 "2019-03-27T06:17:31Z")

</div>

I must admit, I have never tried that. 😳

I first started developing with NodeJS using Linux, so symlinks just felt as a natural tool. I symlinked the packages manually in the beginning, until I came across `npm link`. When we started developing NodeJS at work using Windows, I just kept that approach.

There are some advantages to this approach:

- the workspace is relocatable, because the links always point to the location of your global npm package directory
- you can just "install" it using its package name, no need to know the path to the package

That makes it easier for setup scripts (e.g. for a VSCode workspace) to get everything up and running. 😀

But I will surely give it a try. Maybe it works with relative paths, too.

---

<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:** [27 March 2019 15:11 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/10 "2019-03-27T15:11:52Z")

</div>

> [@kuema](#):
>
> I must admit, I have never tried that

Actually, neither did I until recently.

> [@kuema](#):
>
> symlinks just felt as a natural tool. I symlinked the packages manually in the beginning

That's what I was doing until npm messed it up. It used to work great but if you try that now, at least on Windows, running any kind of npm update/install overwrites the linked installation .. grrr.

> [@kuema](#):
>
> I came across `npm link`

I don't like using that on Windows as it does strange things with the links. It puts an intermediate link in the global area - weird.

> [@kuema](#):
>
> workspace is relocatable

I wouldn't ever want to do that, all my source code is in a fixed place. `c:\src`. Also, my dev node-red installation sits in `c:\src\nr` rather than global with the userDir folder as `c:\src\nr\data`. That way I can easily run up multiple different configurations and even different versions of Node-RED in parallel. Very helpful when working on something like uibuilder where I need to test on different configurations (e.g. with and without projects enabled).

---

<div class="post-metadata">

**Author:** ![realjax](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/realjax/32/895_2.png) [@realjax](https://discourse.nodered.org/u/realjax)\
**Post date:** [8 May 2019 09:50 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/11 "2019-05-08T09:50:56Z")

</div>

Is there more info available on how to get this to work with NodeMon? I have little node developing experience and have no clue how to set this up.

---

<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:** [10 May 2019 12:52 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/12 "2019-05-10T12:52:29Z")

</div>

Here is the `package.json` that I use - this one from the master folder that contains the Node-RED installation, the userDir folder sits in the "data" subfolder and has a similar package.json file.

```json
{
  "name": "node-red-master",
  "version": "0.20.5",
  "description": "Test environment for developing Node-RED flows",
  "main": "node_modules/node-red/red.js",
  "scripts": {
    "start": "nodemon node_modules/node-red/red.js --userDir ./data",
    "inspect": "node --inspect node_modules/node-red/red.js --userDir ./data",
    "update": "npm install --unsafe-perm --production node-red",
    "check": "npm outdated",
    "check-data": "cd data && npm outdated",
    "update-data": "cd data && npm update",
    "check-master": "npm outdated",
    "update-master": "npm update",
    "admin": "node node_modules/node-red-admin/node-red-admin.js",
    "adminui": "start http://localhost:1880/red/",
    "ui": "start http://localhost:1880/ui/",
    "log": "sudo journalctl -u nrlive -f -n 0 -o cat"
  },
  "nodemonConfig": {
    "ignore": [
      "data/*",
      ".vscode",
      "/src/node-red-contrib-uibuilder/nodes/dist",
      "/src/node-red-contrib-uibuilder/nodes/icons",
      "/src/node-red-contrib-uibuilder/nodes/src",
      "/src/node-red-contrib-uibuilder/nodes/templates",
      "/src/node-red-contrib-uibuilder/nodes/uibuilder.html",
      "/src/node-red-contrib-uibuilder/node-src"
    ],
    "watch": [
      "/src/node-red-contrib-uibuilder/nodes/"
    ],
    "env": {
      "NODE_ENV": "development"
    },
    "ext": "js,html"
  },
  "keywords": [
    "node-red"
  ],
  "author": "Julian Knight",
  "license": "MIT",
  "dependencies": {
    "node-red": "^0.20.5",
    "node-red-admin": "^0.1.5"
  },
  "browserslist": [
    "> 0.5%",
    "maintained node versions",
    "last 2 versions",
    "not dead",
    "not ie < 11"
  ]
}

```

Typically, you install nodemon globally `npm install nodemon -g`.

You can use a separate configuration file for it if you like but I prefer to have the settings together in package.json.

You can see the npm start script, that is what starts node-red via nodemon.

---

<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:** [10 May 2019 14:13 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/13 "2019-05-10T14:13:00Z")

</div>

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

1. Install node-red as per instructions on [nodered.org](http://nodered.org)
2. Run it one time so that `.node-red` folder is generated
3. Install own node...
  1. `cd c:\users\me\.node-red` _(or linux equivelant)_
  2. `npm install c:\path_to_my_node`

4. Open VS code & "open folder" created on step 2 e.g. `c:\users\me\.node-red` _(or linux equivelant)_
5. Add following configuration to vscode `launch.json` (press CTRL+SHIFT+P then type launch.json)...

```auto
{
  "version": "0.2.0",
  "configurations": [
   
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Node-red",
      "program": "c:\\path_to_npm_global\\node-red\\red.js",
      "runtimeArgs": ["--preserve-symlinks", "--experimental-modules"],
      "env": {
        "NO_UPDATE_NOTIFIER": "1"
      }
    },
    {
      "type": "node",
      "request": "launch",
      "name": "Debug Current File",
      "program": "${file}"
    },
    { "type": "node", "request": "attach", "name": "Attach to Process", "processId": "${command:PickProcess}" } 
    
  ]
}

```

1. Select "Launch Node-red" from the debug drop down & press `F5`

Hope this helps

Edit...  
Some notes on my vs code setup

- `"NO_UPDATE_NOTIFIER": "1"` Without this, node-red takes over 3 minute startup (corporate proxy issue)
- `--preserve-symlinks` because `npm install c:\folder\my_node` creates a symlink & debug points wont stop on your .js code without it

---

<div class="post-metadata">

**Author:** ![realjax](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/realjax/32/895_2.png) [@realjax](https://discourse.nodered.org/u/realjax)\
**Post date:** [10 May 2019 14:53 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/14 "2019-05-10T14:53:10Z")

</div>

Thanks for the help both of you! I'll dive into this 😊

---

<div class="post-metadata">

**Author:** ![Thepadol](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Thepadol](https://discourse.nodered.org/u/Thepadol)\
**Post date:** [18 March 2020 15:12 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/15 "2020-03-18T15:12:32Z")

</div>

I guess I'm stumbling over my limited knowledge, but I'm not giving up. I've setup a custom node using the sample lowercase conversion code. It works fine. So I want to use it as a setup case to make sure I know how VS code is to work. I've set the breakpoint but it seems to be ignored. Makes me think I need to make sure VS code is looking at the right piece of js in the right place. From the last post maybe symlink is my problem. The node-red is in the same machine as the VS code but there is only an inject node+custom+debug so that should be ok as server side. What am I missing?

---

<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:** [18 March 2020 20:30 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/16 "2020-03-18T20:30:42Z")

</div>

> [@Thepadol](#):
>
> What am I missing?

Hard to say as you haven't really said what you have tried, where things are installed plus a whole bunch of other salient details.

Did you follow the step by step info I posted [above](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/13)?

---

<div class="post-metadata">

**Author:** ![Thepadol](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Thepadol](https://discourse.nodered.org/u/Thepadol)\
**Post date:** [18 March 2020 21:17 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/17 "2020-03-18T21:17:54Z")

</div>

I'll go thru everything again tomorrow (I'm in Rome and at home with the lockdown so have plenty of time). Then I'll update with precise details on what I'm doing (right or wrong). Thanks in advance.

Here's the update:

1. Mac OS installation per [nodered.org](http://nodered.org)
2. .node-red folder exists as ~/.node-red
3. my development folder is ~/My Documents/Dev/node/node-red-contrib-hex2buffer. With NPM install I find in the /usr/local/lib/node\_modules  
lrwxr-xr-x 1 thepadol wheel 55 Mar 19 13:39 node-red-contrib-hex2buffer -\> ../../My Documents/Dev/Node/node-red-contrib-hex2buffer
4. in launch.json I coded:  
"type": "node",  
"request": "launch",  
"name": "Launch Node-red",  
"program": "/usr/local/lib/node\_modules/node-red/red.js",  
"runtimeArgs": ["--preserve-symlinks", "--experimental-modules"],  
"env": {"NO\_UPDATE\_NOTIFIER": "1"}

As far as I can tell vs code launches node-red correctly. If I select attach I can see the process running and recognized by vs code (just for purposes of looking at what vs code thinks is a node process).

What is hex2buffer.js? Currently a test custom node for purposes of verifying vs code. The source is  
module.exports = function(RED) {  
function hex2buffernode(config){  
RED.nodes.createNode(this,config);  
var node = this;  
node.on('input', function(msg) {  
msg.payload = msg.payload.toLowerCase();  
node.send(msg);  
});  
}  
RED.nodes.registerType("hex2buffer",hex2buffernode);  
}  
the flow is

 ![Screen Shot 2020-03-19 at 4.26.44 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/902d0f05157be70c8eb864745570d489eb9ef419.png)  
and the results:  
 ![Screen Shot 2020-03-19 at 4.30.30 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/22821750403f6d8bc4e5d354af28406936e41892.png)

When using launch "node-red" from vs code I get the flow editor and can inject as expected. However no matter what breakpoint I place in hex2buffer.js seems to be ignored. I suspect that i must be placing the breakpoint in the wrong file or version of hex2buffer.js. Since the node-red launch shows the right flow I assume that I installed the custom node properly.

What other salient info can I provide? thanks.

---

<div class="post-metadata">

**Author:** ![Thepadol](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Thepadol](https://discourse.nodered.org/u/Thepadol)\
**Post date:** [20 March 2020 11:09 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/18 "2020-03-20T11:09:27Z")

</div>

Guess I'm replying to myself, but it's progress. I decided to start from scratch on a different computer in the hope of cleaner environment (by experience it's a possible factor). Initially the behavior looked the same. Between considering a bug in the software vs being a user (me) problem, 99% it's the latter. VS code was stopping at node-red startup so that was a good sign. So I said to myself what am I assuming that I shouldn't? How about the breakpoint itself? I mean VS code allows you to set breakpoints but it assumes you know when that breakpoint is to occur and that depends on what the code is doing. A good haha moment. I selected a different point and it now stops as I would have expected. Tried on the original PC and it does the same. The original breakpoint only works at the point in time when the custom node is defined, not when executing. I think this sorts out my problem. Thanks for showing the way on VS code.

---

<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 14:18 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/19 "2020-08-21T14:18:47Z")

</div>

Hi,  
Were you able to debug your custom node successfully.  
I am trying but any break point is showing as "unbound"

Regards,  
Mathur

---

<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 14:25 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/20 "2020-08-21T14:25:29Z")

</div>

HI Steve,  
I tried the steps you mentioned above. When I start the debugger, it automatically starts nod-red. I have no place to select "Launch Node Red" and any break point I add in my custom node code, is showing as "unbound" and VC code is ignoring them. Can you 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 15:44 UTC](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/21 "2020-08-21T15:44:42Z")

</div>

Hi @mathurv, did you follow [this](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/13) step by step? Obviously, you will need to set **your** file paths.

It still works (I refer to it every time I set up another machine for debugging)

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