Using Custom Javascript objects from Node-Red flows.

1,525 views
Skip to first unread message

ajaykas...@gmail.com

unread,
Jul 19, 2016, 11:03:55 PM7/19/16
to Node-RED
I have been evaluating the use of node-red. I was wondering other than the OOB javascript objects such as Buffer, Date and other OOB javascript objects that are available to the function node in node-red, what would be the ideal way to expose custom javascript objects to be available to the function node in a node-red flow?  

If that is possible, is there a way to pre-create instances of these custom javascript objects in most cases singleton instances on load of node-red flow  and make them available globally to various flows?

Thanks,
Ajay

Mike Blackstock

unread,
Jul 20, 2016, 12:05:31 AM7/20/16
to node...@googlegroups.com
There's a property in the settings.js file called functionGlobalContext (see mention of it under Node Configuration in the docs at http://nodered.org/docs/configuration) where you can make any JavaScript object globally available to all function nodes.  Here's the comments in my settings.js file:

    // Anything in this hash is globally available to all functions.
    // It is accessed as context.global.
    // eg:
    //    functionGlobalContext: { os:require('os') }
    // can be accessed in a function block as:
    //    context.global.os

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

Hope that helps.

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

ajaykas...@gmail.com

unread,
Jul 20, 2016, 5:26:24 PM7/20/16
to Node-RED
Thanks Mike for your response. I think I got it. However where where would the javascript files that contain the custom javascript objects need to be placed in the node-red directory structure?

Thanks,
Ajay.

Mike Blackstock

unread,
Jul 20, 2016, 6:14:23 PM7/20/16
to node...@googlegroups.com
Cleanest way would be to find or create a node.js module containing the custom code you want to add, then install them using the npm install or npm link.  The new code would appear in the 'node_modules' directory within the 'node-red' directory like other modules it depends on.  There are probably other ways, but may involve messing with directories you shouldn't really be messing with.

Assuming you're familiar with node.js development and npm?  If not you can google for some blogs and tutorials to get started - probably worth while.  e.g. to create a node module: https://docs.npmjs.com/getting-started/creating-node-modules.

Once you've got a node module with your code in it running and tested, you need to get it into the node_modules directory.  To do this you can create a symbolic link (ln -s) to your directory to your node red `node_modules` directory, use the npm link command (https://docs.npmjs.com/cli/link) or you can publish it to the global npm repository for others to use then install it.

If you create a module called 'my-custom-module' you would then add

functionGlobalContext {
   mymodule:require("my-custom-module")
}

then access it as 'context.global.mymodule' in your code.

Hopefully this is enough to get you going in the right direction.

ajaykas...@gmail.com

unread,
Jul 21, 2016, 11:22:56 AM7/21/16
to Node-RED
Thanks Mike for taking the time to respond. I didn't think it was going to be so involved to expose custom javascript objects. I am not very familiar with node.js, but something new to learn. Will follow the directions you have called out and hopefully I can get it to work as you have suggested.

Thanks,
Ajay.

Mike Blackstock

unread,
Jul 21, 2016, 12:32:58 PM7/21/16
to node...@googlegroups.com
Not sure if this is what you want, but you might be able to do something quick & dirty like create a Javascript object in a function node at the beginning of a flow's execution, then assign it to global context for other functions to use later.   Wouldn't give you access to more node.js or OS functionality or external libraries though, your new object would still be limited to what node-red provides in a function by default.  Maybe this is what you had in mind?

ajaykas...@gmail.com

unread,
Jul 22, 2016, 6:36:36 PM7/22/16
to Node-RED
Thanks Mike I created a test custom module and installed it via NPM and was able to get it to work. Thanks for your inputs. As far as what my function or object would be doing is actually managing and storing binary data which needs to pre-processed before forwarding to the next node and I need to apply some rules and transformations before the data can be used. Although I can do it using a function node, I would be repeating this function in many of my flows and would like to access it from any flow, without having to write the code over and over again. Plus I may have to use the underlying OS functionality.

Thanks,
Ajay.

Ben Hardill

unread,
Jul 23, 2016, 5:45:32 AM7/23/16
to Node-RED
If it's something that your going to use over and over I've be tempted to wrap it as a node-red node as this will run a bit quicker than a function node.

It's probably only going to take another 4 or 5 lines of javascript and a little bit of HTML.

The docs for writing nodes can be found here http://nodered.org/docs/creating-nodes/
Message has been deleted
Message has been deleted

M.R. Smith

unread,
Mar 17, 2017, 1:49:17 PM3/17/17
to Node-RED
Trying 3rd time, asking a question about settings.js, each one has been deleted (why?)

We too are trying to access a node.js module (sphero) under Node.RED and are not getting the functionGlobalContext: { } ... correct.

Questions related to the Docker Node-RED image we used as the base. 
 
Where should I make a change to settings.js when trying to "use" an imported node.js module? 

1) can this functionGlobalContext:{ } go in the function itself, or MUST it go into settings.js file? 

We have some Node-RED flows which defaulted to /data, so we have several settings.js files:

 /usr/src/node-red/node_modules/settings.js
 /usr/src/node-red/node_modules/red/runtime/settings.js

then under /data where contributions to Node-RED stated they should go for FRED and one other:
 
 /data/node-red/node_modules/settings.js
 /data/node-red/node_modules/red/runtime/settings.js

So which one(s) do we edit? 

Tx, Mark
PS - I changed from my default mark...@snkmail.com e-mail in case this list doesn't allow posts from snkmail.com


Nick O'Leary

unread,
Mar 17, 2017, 2:17:36 PM3/17/17
to Node-RED

Hi, I responded to your message to the list owners - your other messages were not deleted. They had been held for moderation as Google suspected them of being spam. I approved your message and all future posts so they shouldn't get held again. Apologies for the inconvenience. You'l see your original message has been posted to the list - I certainly received a copy.


`functionGlobalContext` is a user setting that must go into the settings file your node-red instance is using. Node-RED will only be using one settings file; it logs exactly which it's using on startup. As to how you set that on FRED specifically, I'll have to defer to Mike or someone else from the FRED team to chip in.

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+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

M.R. Smith

unread,
Mar 17, 2017, 6:21:01 PM3/17/17
to Node-RED
tx - got it. Appreciate explanation on the group thing... and look forward to help understanding which of the 5 different settings.js files is active when running the Docker image of Node-RED with the FRED flows added to /data


On Friday, March 17, 2017 at 2:17:36 PM UTC-4, Nick O'Leary wrote:

Hi, I responded to your message to the list owners - your other messages were not deleted. They had been held for moderation as Google suspected them of being spam. I approved your message and all future posts so they shouldn't get held again. Apologies for the inconvenience. You'l see your original message has been posted to the list - I certainly received a copy.


`functionGlobalContext` is a user setting that must go into the settings file your node-red instance is using. Node-RED will only be using one settings file; it logs exactly which it's using on startup. As to how you set that on FRED specifically, I'll have to defer to Mike or someone else from the FRED team to chip in.

Nick



On Fri, 17 Mar 2017, 17:49 M.R. Smith, <7tro...@gmail.com> wrote:
Trying 3rd time, asking a question about settings.js, each one has been deleted (why?)

We too are trying to access a node.js module (sphero) under Node.RED and are not getting the functionGlobalContext: { } ... correct.

Questions related to the Docker Node-RED image we used as the base. 
 
Where should I make a change to settings.js when trying to "use" an imported node.js module? 

1) can this functionGlobalContext:{ } go in the function itself, or MUST it go into settings.js file? 

We have some Node-RED flows which defaulted to /data, so we have several settings.js files:

 /usr/src/node-red/node_modules/settings.js
 /usr/src/node-red/node_modules/red/runtime/settings.js

then under /data where contributions to Node-RED stated they should go for FRED and one other:
 
 /data/node-red/node_modules/settings.js
 /data/node-red/node_modules/red/runtime/settings.js

So which one(s) do we edit? 

Tx, Mark
PS - I changed from my default mark...@snkmail.com e-mail in case this list doesn't allow posts from snkmail.com


--
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.
Reply all
Reply to author
Forward
0 new messages