Node Red Function Library

1,669 views
Skip to first unread message

Peter Scargill

unread,
May 30, 2016, 5:08:08 PM5/30/16
to Node-RED
Node Red Function library - that is when you are writing a function there's an option to open the library.

I know nothing about this and I'm not having any joy Googling a precise definition of it's use.  My needs are that I'm using a couple of large JS functions (RGB LED handling) and I'm ending up using them over and over in my own functions - got to be a better way and I'm assuming this library is it but I can't find info on it??

Can anyone point me in the right direction?

Pete.

Nicholas O'Leary

unread,
May 30, 2016, 5:21:41 PM5/30/16
to Node-RED Mailing List
You can save a function by selecting 'Save to library' option and then retrieve it using the 'Open Library...' option. Not much more to it than that. Try it out and see.

The functions (and indeed, anything that has a library type such as the Template node) get stored under the 'libs' directory in your node-red user directory.

Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
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.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
For more options, visit https://groups.google.com/d/optout.

Peter Scargill

unread,
May 30, 2016, 6:21:50 PM5/30/16
to node...@googlegroups.com

What I meant to get at – so this is just a simple snippet store then – not a means to make a single copy of a function globally available. Just clarifying?

You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/i24WzUoRgTE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+u...@googlegroups.com.

Nicholas O'Leary

unread,
May 30, 2016, 6:31:40 PM5/30/16
to node...@googlegroups.com

Correct, a snippet store is a good description. Not a library in the programmatic sense.

N

cflurin

unread,
May 31, 2016, 9:15:10 AM5/31/16
to Node-RED
You can also save a function in a global variable. E.g. I've defined a function for saving values in a global object.

1. define the "library"-function with a function node:

global.set('setState', function(msg) {
    var n = msg.payload.name;
    var c = msg.payload.characteristic;
    var value = msg.payload.value;

    var state = global.get('state') || {};
    var a_state = state[n] || {};
    a_state[c] = value;
    state[n] = a_state;
    //node.warn(state);
    global.set('state', state);
});

2. use the "library"-function in your nodes.

global.get('setState')(msg);

Julian Knight

unread,
Jun 1, 2016, 3:20:30 PM6/1/16
to Node-RED
Why not put that in settings.js? Then you know it is always available even at startup and you don't have to worry about timing issues.

Nicholas O'Leary

unread,
Jun 1, 2016, 4:31:46 PM6/1/16
to Node-RED Mailing List
You really should not be storing Functions into context like that - unless they are provided in your settings file. When we make it persistable, any property that isn't JSON encodable will get lost. We will look at actively policing that in the future - but as we've not policed it from the start, we'll have to add a deprecation warning first if we spot it being doing it...

Nick

--

cflurin

unread,
Jun 2, 2016, 4:23:54 AM6/2/16
to Node-RED
I see, thanks for warning me. Good to know what you are planing for the future.  Although I don't' think that building a library in setting.js (functionGlobalContext?) is a ideal solution. That means I've to restart node-red every time I change a function in setting.js?

Nicholas O'Leary

unread,
Jun 2, 2016, 5:11:42 AM6/2/16
to Node-RED Mailing List
No it is not an ideal solution.

The original question in this thread was about what the existing library function was, which is more of a snippet library that allows you to save and recall useful functions you've used. It is not a library of functions that are available to be invoked from any Function node.

What you're looking for is not something that's come up before - the ability to have some flow-wide 'utility' functions that Function nodes can call, without having to resort to updating settings.js.

Nick

On 2 June 2016 at 09:23, cflurin <luigi.ci...@gmail.com> wrote:
I see, thanks for warning me. Good to know what you are planing for the future.  Although I don't' think that building a library in setting.js (functionGlobalContext?) is a ideal solution. That means I've to restart node-red every time I change a function in setting.js?

--

cflurin

unread,
Jun 2, 2016, 9:20:03 AM6/2/16
to Node-RED
Maybe I misunderstood the first posting but Peter wrote "My needs are that I'm using a couple of large JS functions (RGB LED handling) and I'm ending up using them over and over in my own functions". So I thought saving the function in a global would be a practical solution. Anyway as you mentioned this will not be possible in the future, so - when the global would be persist - one solution could be to store the global functions in a file on closing and read them back on init. Just now I do the same with the states of my variables.

Julian Knight

unread,
Jun 2, 2016, 6:26:02 PM6/2/16
to Node-RED
You can of course combine both ideas. Since we are using Node.JS, you make your utility functions as node modules and simply require them in settings.js, each restart of NR will pick up the latest version.

That has even more benefit because you can now use npm to manage your utility modules. This is actually quite an interesting idea. I do have some small utility functions I use that could come out of NR. I actually probably only use them in one place as I've been simplifying things further recently but they would still be better in separate modules since they do standard things like calculate the dew point, perceived temperature and sea-level equivalent pressure. All of which I use to normalise data coming from different sensors.


What I think Nick was beginning to think about is a kind of "lazy loading" arrangement where the modules are only loaded when they are actually needed? This might be interesting in a few cases where you wanted to work with complex JavaScript but didn't need to have it loaded most of the time or if the code changes a lot. But seriously overkill for the purposes of maintaining fairly static utility functions.

Nicholas O'Leary

unread,
Jun 2, 2016, 6:37:04 PM6/2/16
to Node-RED

Julian,
No, I was thinking of being able to edit that utility code in the Node-RED editor and not force the user to restart each time they had to update them. If the settings file was the only way to do it, then it would be pretty unusable in cloud environments where you can't easily edit files in disk.

But regardless, nothing we have plans to look at currently.

Nick


--

ajaykas...@gmail.com

unread,
Jul 22, 2016, 3:01:27 PM7/22/16
to Node-RED
Is there a way to install the custom node module containing multiple utility functions without using the NPM repository? Is it possible to install this package locally so I can reference it within node-red,  as is this is private code and would not like to publish within the NPM repository?

Thanks,
Ajay.

Timur Fatykhov

unread,
Jul 22, 2016, 3:49:33 PM7/22/16
to Node-RED
I do not see why you cannot do that. Do not metnion dependency in package.json for that module but push it manually in case you are using cloud or just copy into proper location if you are using local instance.

ajaykas...@gmail.com

unread,
Jul 22, 2016, 4:14:24 PM7/22/16
to Node-RED
Thanks Timur. I just did that and managed to install my custom test module into the node_modules contained under the node_red folder. Also I updated the settings.js to indicate the new custom module as suggested in earlier posts.

so in all simplicity my code for the custom module is as simple as below and is contained in app.js

var MyCustomObject = function () {};

MyCustomObject.prototype.customPrint = function (msg) {
    console.log("My Node Red Test Module..." + msg);
};

module.exports = new MyCustomObject();

and my settings.js file contains the following changes.

functionGlobalContext: {
        mfn:require('MyFirstNodeJSApp')
        // os:require('os'),
        // octalbonescript:require('octalbonescript'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
    },

When I try and invoke the customPrint using the context.global.mfn.customPrint("test"); i get the following error and not sure what I am doing wrong.

function : (error)TypeError: Cannot read property 'customPrint' of undefined

Thanks,
Ajay.

ajaykas...@gmail.com

unread,
Jul 22, 2016, 4:43:35 PM7/22/16
to Node-RED
Sorry jumped the gun, I was able to fix it. It looks like on the windows install of node red. node-red was installed in "c:\users\ajayk\appdata\roaming\npm\node_modules..." and "c:\users\ajayk\.node_red". The latter worked. Why is this installed in two locations.

Thanks,
Yogesh

Nicholas O'Leary

unread,
Jul 22, 2016, 4:53:51 PM7/22/16
to Node-RED Mailing List
No, node-red is installed in one location (c:\users\ajayk\appdata\roaming\npm\node_modules..) and your user data directory is in another location (c:\users\ajayk\.node_red).

Your settings file will be in the second of these, so any modules you want to require within it must be local to it.

Nick

--

Dave C-J

unread,
Jul 22, 2016, 5:36:38 PM7/22/16
to node...@googlegroups.com

If you want to package your node(s) but distribute them locally / privately, you can create them as any other node, then use npm pack (rather than npm publish, to create a .tgz  file that you can copy around and then use npm install yourfile.tgz (from within your user directory as per Nick's note) to install as per any other node.

ajaykas...@gmail.com

unread,
Jul 22, 2016, 6:25:15 PM7/22/16
to Node-RED
Thanks that would work well for me as I would need to share it with my team.

Thanks,
Ajay.

ajaykas...@gmail.com

unread,
Jul 22, 2016, 6:30:35 PM7/22/16
to Node-RED
Hi Nick,

Would this be similar in the linux system as well and I understand the folder structure is very different on Linux. Eventually the custom module I am building will be deployed in a device (Multitech's Conduit) currently hosting node-red, which I think has an underlying OS which is linux based. What is the best way to find out via command line which folder to deploy to?

Thanks,
Ajay.
Reply all
Reply to author
Forward
0 new messages