How to proper debug custom node

#1

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?

#2

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 to run node-red and have it automatically restart whenever it sees changes to the node files.

2 Likes
#3

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) :slight_smile:

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.

#5

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 :slight_smile:
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

#6

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

#7

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

Works fine on Windows, too.

#8

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

2 Likes
#9

I must admit, I have never tried that. :flushed:

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

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

1 Like
#10

Actually, neither did I until recently.

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.

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.

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