Just created node to read function from file

810 views
Skip to first unread message

Emil Oberg

unread,
Jan 26, 2015, 6:41:20 AM1/26/15
to node...@googlegroups.com
Hi,

I just created a node, to make flow creation a bit easier, which may be of interest.

As awesome as Node-RED is, it is not a code editor/IDE (and shouldn't be). This small node works just as the core "function" node, only difference is that this will load the javascript to be executed from a file on your file system. Instead of having to write your Javascript code in that small textfield in your browser you can use Sublime/IntelliJ/WebStorm/whatnot. The file will be read every time the node is called, so there's no need to redeploy or restart Node-RED.

When done developing, if you want to, you may copy the script to the ordinary script function to make it shareable.


Feedback and pull requests are very much welcome.

Does this make flow creation a easier?

Dave C-J

unread,
Jan 26, 2015, 8:16:38 AM1/26/15
to node...@googlegroups.com
Hi , thanks - yes I'm sure some people will find this useful.

Emil Oberg

unread,
Jan 26, 2015, 1:43:40 PM1/26/15
to node...@googlegroups.com
Got some great advice by private email. The node is now updated and you now have the option to
  • read the file when NR is started/redeploed (better for performance), or
  • every time the function is invoked (great when developing - no need to restart/redeploy NR when code is changed)

Scott Penrose

unread,
Jan 27, 2015, 4:45:52 AM1/27/15
to node...@googlegroups.com
Looks great. I was contemplating the same thing. I was going to have it import into Function nodes, but this is better.

One thing I would like to see is some form of input parameter. Maybe even just one - so you could even encode multiple values, or comma separated parameters. That way one JS file could be used multiple times with different parameters.

ryan...@gmail.com

unread,
Jan 27, 2015, 5:32:19 AM1/27/15
to node...@googlegroups.com
Will make my life easier, thank you :)

Lorenzo Maiorfi

unread,
Jan 27, 2015, 6:45:36 AM1/27/15
to node...@googlegroups.com
I was probably the first one on this google group asking for such a great feature...thanks Emil !!!!

--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Emil Oberg

unread,
Jan 27, 2015, 8:36:30 AM1/27/15
to node...@googlegroups.com
Hmm, I don't really understand, could you clarify?

This node works just like the out of the box functions node, and you have access to the msg object as an "input" parameter. 

So you can easily do like this:
if (msg.payload === '1') {
 
// Do all magic for payload 1 here
 
return { payload: 'I got ONE as input' }
} else if (msg.payload === '2') {
 
// Do all magic for payload 2 here
 
return { payload: 'I got TWO as input' }
}


O
if (msg.topic === '1') {
 
// Do all magic for topic 1 here
 
return { payload: 'I got ONE as input' }
} else if (msg.topic === '2') {
 
// Do all magic for topic 2 here
 
return { payload: 'I got TWO as input' }
}


or whatever set on the msg object by the previous node
if (msg.whatever === 'something') {}


Dave C-J

unread,
Jan 27, 2015, 9:00:30 AM1/27/15
to node...@googlegroups.com
I "think" he wants to be able to pass in configuration parameters to your script loading function... but (IMHO) I think that would then break the whole model - in that you could no longer cut/paste the edited file into a "normal" function block and vice versa.

Julian Knight

unread,
Jan 27, 2015, 11:59:14 AM1/27/15
to node...@googlegroups.com
You must have read my mind! Any my recent comments as my poor coding capabilities means lots of little changes :( I much prefer to work with a local, favourite editor too so now I can work with a familiar WinSCP/Brackets toolchain.

Great work thanks.

Scott Penrose

unread,
Jan 27, 2015, 4:07:56 PM1/27/15
to node...@googlegroups.com

Den tisdag 27 januari 2015 kl. 10:45:52 UTC+1 skrev Scott Penrose:
Looks great. I was contemplating the same thing. I was going to have it import into Function nodes, but this is better.

One thing I would like to see is some form of input parameter. Maybe even just one - so you could even encode multiple values, or comma separated parameters. That way one JS file could be used multiple times with different parameters.

On Wednesday, January 28, 2015 at 12:36:30 AM UTC+11, Emil Oberg wrote:
Hmm, I don't really understand, could you clarify?

Hi Emil. Almost every node has some form of input - e.g. you can set the IP number in Ping, or the filename in files. However when you create a sub-flow, or in this case the external file, you have no way to add that type of data. You can fake it by passing in topic="set_filename", payload="the_filename.txt", and fire an event that sets that data and records it in context - but it has some down sides (hard to understand, especially for beginners, and has a race condition at startup). My question is this - if Files need a Filename, why would other nodes need nothing. And I was just thinking some generic way of passing in a single param to this external file would make it more useful, and certainly doesn't break copy/paste, any more than it does with any other Node.

Scott 

Dave C-J

unread,
Jan 27, 2015, 6:52:05 PM1/27/15
to node...@googlegroups.com
Hi Scott,

err - how can it not break copy/paste into the existing function node when the existing function node doesn't accept any other parameters ? So you hvae no where to put these extra parameters in the existing function node. Everything is passed in on the msg object - and that is the same with Emil's node (afaik).

Scott Penrose

unread,
Jan 27, 2015, 6:55:40 PM1/27/15
to node...@googlegroups.com


On Wednesday, January 28, 2015 at 10:52:05 AM UTC+11, Dave C-J wrote:
Hi Scott,

err - how can it not break copy/paste into the existing function node when the existing function node doesn't accept any other parameters ? So you hvae no where to put these extra parameters in the existing function node. Everything is passed in on the msg object - and that is the same with Emil's node (afaik).

I don't really understand what you mean above? You are saying into function node, do you mean into the text area of the function node? I am only talking about parameters. Certainly if I click in the UI a node, copy, paste, I get the parameters copied too. Parameters, like Filename in File, IP address in Ping, DB file in any of the DB, Timeout in Timer etc etc are important parts of the NR nodes. That is all I meant. Maybe I am not being clear, sorry, probably I am using the wrong terminology somewhere.

Scott 

Dave C-J

unread,
Jan 27, 2015, 7:12:17 PM1/27/15
to node...@googlegroups.com

Ah right. I thought the idea of this was to replace/substitute for the existing function node, but allow editing in an external browser...

So edit the file in whatever editor you want, but point to that for from these nodes.

Ideally you would want (at least in my world) to be able to take the contents of that file and paste it into an existing function node's edit window and that function to behave the same... I think.

Scott Penrose

unread,
Jan 27, 2015, 7:22:25 PM1/27/15
to node...@googlegroups.com
On Wednesday, January 28, 2015 at 11:12:17 AM UTC+11, Dave C-J wrote:

Ah right. I thought the idea of this was to replace/substitute for the existing function node, but allow editing in an external browser...

I see our confusion now :-)

In a Function - each and every instance of that function is itself the code. So if I copy/paste a function, I can now go into the second function and change it a bit - maybe you are just changing the "topic" going out.

So edit the file in whatever editor you want, but point to that for from these nodes.

However in this object you copy paste a set of these, they are all pointing to the same file, how do you change them? You can't. You have to have 10 files for 10 nodes. That is acceptable of course. Perfectly valid. I was just suggesting that because you have a "shared' file possibility in this case, that some form of parameter into that file would be nice.

It is completely unnecessary in the existing function code, as there is no way to "share" code. While this is an external file so it would be possible.

With such a tool I could do something like this. Write an external function that intelligently parses my incoming data, formats the date correctly, cross references and IP number, looks up the valid name for an ID range (making it up as a I go) and then output it into the payload JSON structure ready for MQTT, or DB or what ever... but now in case 1 I want to use the topic "sensordata" while in node 2 I want to use the topic "systemstatus" - todo that with this new one I would not be able to share all my lovely work above, but if I had some form of parameter I have just made the module 10 times more useful (well... to me) :-)

Having the ability to load other files in (require style) inside this node existing function and this one, based on the data directory or similar would also solve this problem. Mind you if you had the ability easily to load file from within existing function, it also means you don't need this new node, as you would be able to just point to that external file.

My confusion was over the idea of making it shared file, while I see now the idea was to have one file per node, and therefore no need for parameters.

Thanks for you patience.

Scott

Nicholas O'Leary

unread,
Jan 27, 2015, 7:30:32 PM1/27/15
to node...@googlegroups.com

If you add 'require' to functionGlobalContext in your settings file, then you would be able to require arbitrary js files as part of a function node and invoke then

The required module would have to export a function that takes the incoming message and returns the result. It would also be subject to the same sandbox as the current function node.

I'm not advocating this approach... Just pointing to its existence.

Nick


Dave C-J

unread,
Jan 27, 2015, 7:34:37 PM1/27/15
to node...@googlegroups.com

OK. On same page now. And no. I think that is a step too far from where we want to be. The core idea is not to edit files.

Emil Oberg

unread,
Jan 29, 2015, 4:34:28 AM1/29/15
to node...@googlegroups.com
Just to clarify, Dave is completely right describing the intentions of this node.

The reason for me to create the node (which frankly isn't anything advanced at all, it's pretty much just a merge of the existing file node and function node) is just what Dave said; I wanted to create a drop in substitute for the function node but be able to use WebStorm (which is my weapon of choice for Javascript development) and all the goodies such as code completion, linting, syntax highlighting etc. Because let's face it, Node-RED is awesome but not an IDE ;)

The way it works right now you can copy code from the "external" javascript file of the file function node and paste it into the out-of-the-box function node and vice versa, and as understand the intentions of Node-RED the "social development" idea of being able to export/import flows is a fundamental thing. Therefor I won't be adding any functionality which breaks the compatibility with the ootb functions node. But anyone is of course more than welcome to fork it and create magic!

Dave C-J

unread,
Jan 29, 2015, 5:18:00 AM1/29/15
to node...@googlegroups.com
+1  :-)

Julian Knight

unread,
Jan 29, 2015, 5:42:15 AM1/29/15
to node...@googlegroups.com
Spot-on Emil, this will be really nice for those times we are prototyping code with lots of little changes. Cuts out a load of copy/paste activity :)

Be nice to have this for templates as well as they are the other nodes that can have a lot of text in them. :)

Dave C-J

unread,
Feb 26, 2015, 6:07:52 PM2/26/15
to node...@googlegroups.com
PS - just a heads up - in the latest release v0.10.4 - we have now switched over to the ACE editor for the function, template and comment nodes... this gives much better syntax highlighting and checking as well as folding etc etc... so hopefully a much nicer in browser dev experience ;-)

Nicholas O'Leary

unread,
Feb 26, 2015, 6:15:07 PM2/26/15
to node...@googlegroups.com

No we haven't.... Ace was merged after the release was published.

It will be in the next release.

Nick


On Thu, 26 Feb 2015 23:07 Dave C-J <dce...@gmail.com> wrote:
PS - just a heads up - in the latest release v0.10.4 - we have now switched over to the ACE editor for the function, template and comment nodes... this gives much better syntax highlighting and checking as well as folding etc etc... so hopefully a much nicer in browser dev experience ;-)

--

Dave C-J

unread,
Feb 26, 2015, 6:16:51 PM2/26/15
to node...@googlegroups.com
doh... !  I must stop using git pull - must use npm :-)

Julian Knight

unread,
Mar 2, 2015, 4:35:28 AM3/2/15
to node...@googlegroups.com
:)

Using npm direct from github to get the best of both! Of course that would leave you with the same problem!

Dave C-J

unread,
Mar 2, 2015, 5:39:45 AM3/2/15
to node...@googlegroups.com
Julian

as Nick has mentioned elsewhere in the blog - we are NOT recommending people install direct from git anymore - unless they are developers. We are looking to introduce a build step into the dev process that will help shrink some of the browser side code and allow some other optimisations - but will require anyone running from git to also run the full build step locally. It's not hard - but is something most users shouldn't need to do - hence the recommendation to use the pre-built release versions.

Julian Knight

unread,
Mar 2, 2015, 6:05:39 AM3/2/15
to node...@googlegroups.com
Thanks Dave.

The nice thing about using NPM this way is that it is a trivial change to move between versions. Update the JSON and "npm update". So when builds calm down and you introduce the build step - or indeed when my end stabilises, I can easily swap to the release version.
Reply all
Reply to author
Forward
0 new messages