A way to export the functions of your Node Red Flows to a .js file

Hello Folks,

I am working on a bigger academic project atm and was looking for a way to export the code in my function Nodes to a .js file.

Main Reasons:

  1. Easier Versioning
  2. external editing in e.g. VSC

I am using basically some Core Nodes for this project and ,most of all, function nodes.
With around 5000 lines of function-node-code copy and paste is no option, evidently.

So I come up with this:

I needed this immediatly, so it is a quick-n-dirty-solution but it works fine so far.

You can simply pass in your complete flows.json.

Function Nodes then are extracted and written to a JS as js functions with results looking like this:

I expect to improve this and clear thing up in the future, add some more features, but for now this is what i needed instantly.

If you have ever been looking for something like this. You are welcome :slight_smile:

If not, thats okay for me :upside_down_face:

Have a nice Sunday, cheers from cold Germany

2 Likes

If you are using Linux, then an alternative is to do this.

  1. In settings.js set flowFilePretty: true
  2. Before you commit your flows file to GIT, run the simple script below.
  3. Add the file yourflowfile.json.formatted to your git repository.
  4. The formatted file will show the functions expanded, so that the differences will show you the changes.

This is the script, which assumes the flow file starts with the flow. If yours doesn't then the change to allow it should be obvious;

#!/usr/bin/env sh

# generates formatted versions of node red flow files matching flow*.json
# in files flow*.json.formatted

# NOTE must have flowFilePretty: true in settings.js

for f in flow*.json; do
  echo $f
  sed -r 's/\\n/\n/g' "$f" > "$f.formatted"
done
# remove any cred files converted
rm *_cred.json.formatted
1 Like

Thank you for this simple and effective solution!
In fact I, am using different systems, Linux and Windows.
The trick is, when I thought about doing this, I was aware of the fact that any machine that runs NR, not regarding the OS must have node.js installed.
So when I now export the flows with the JS program I wrote, which works on any OS that runs node.js, I get them filtered by functions, and only them, and have them converted, well 'wrapped' in actual js-functions that an IDE interpreter doesnt promt problems on, all in one file. That is JS and not JSON.
So i can use VSC or atom for development (auto-completion, show function/class constructor arguments, etc.) independently. Then I copy the function-node Id, search for it in the NR Editor, copy the "wrapped function" i edited externally back into the function node an let it run.

For me, the main feature of this and what i wanted, is getting the functions in a very uncluttered way that i can work on without any overhead, and i can export only the content of function nodes (automatically if I desire to), on any OS :slight_smile:

Interesting idea. May indeed help on visualizing code in a slightly different way as it will give you a view of (part of) your logic in a "single page". As usual in programming you can discover bugs, improve code or simply understand it better when you have a different view or perspective. Most of my flows use function nodes sparsely and they normally have small bits of code only. Anyway exporting the function code this way help developers that "abuse" (no criticism here) of function nodes.

What you will get when the function node has code in the tabs setup, onstart and onstop ?

Let´s say you edited some code in the resulting file. How you will then update Node-RED flow ? Copy - paste back to the flow ?

Absolutely!

And good Question! Until now i have never used start stop and setup because it wasnt necessary in my particular scenario. And when importing js modules I add them to my settings and import them to the "main"-node function by

const fs = global.get('fs')

If I remember it right, this Import Module feature in the function setup is not that old.

However, the function node when exported to a flowfile turns out to be:

{
stuff1:"",
name:"superfunction",
stuff2:"";
"func":"msg.payload= 'This ist the interesting Stuff';\nreturn msg;",
"initialize":"msg.payload = 'The StartFunction';\nreturn msg",
"finalize":"msg.payload = 'The EndFunction';\nreturn msg",
"morestuff":""
}

that means, it should be no problem to not only extract the 'func' part, but to wrap two functions in the function named init() & finit().

However2, maybe I can include this one afterwork evening but yet this does not target to gain execable js code from NR Nodes. For the sake of Versioning or copying it back to the flow, you are right, s.o. who uses this could need that.

Yes when I edit, at the moment, I copy and Paste back manually.
Well if there are 100 functions in the file, you dont work on all 100 at the same time, but on one or two.
For that I have the Node ID as a comment above the function by which i find the Node in the NR editor, and paste the complete function back in. but youre right a reimport feature would be awesome :smiley:

However3, I needed a solution for editing and versioning and sharing logic with people who don't use node red and therefore have no instance installed. Instead of copy'n'paste 2 hours, I decided to spend two hours writing this script. But I appreciate your Input, that hasnt been coming to my mind like that, yet. When I have more time, this is something definitely worth considering to implement.

2 Likes