Can code in a function node use other NPM modules?

783 views
Skip to first unread message

Neil Kolban

unread,
Mar 16, 2016, 10:14:27 PM3/16/16
to Node-RED
Imagine I find a nice NPM module that I like ... can I install it to be used with Node-RED and then leverage it within JavaScript in a "function" node?

For example, can I install npm module "XYZ" and then in my function node implement:

var xyz = require("XYZ");
xyz
.doSomething(msg.payload);

Is there anything suspect about doing this kind of thing?

If I can do this, the next question becomes when would I choose to "just code it up" as opposed to taking the time to make a new node type out of it?

Neil

Nathanaël Lécaudé

unread,
Mar 16, 2016, 11:18:57 PM3/16/16
to Node-RED
you can't do this directly for security reasons, but you can check your settings files, in the global context you can import external libraries that you'll then be able to use in a function node.

Nicholas O'Leary

unread,
Mar 17, 2016, 2:12:07 AM3/17/16
to Node-RED

Neil,

Here's a link to the Writing Functions docs that shows how to do this with the 'OS' module as an example.

http://nodered.org/docs/writing-functions#global-context

Nick


On Thu, 17 Mar 2016, 03:18 'Nathanaël Lécaudé' via Node-RED, <node...@googlegroups.com> wrote:
you can't do this directly for security reasons, but you can check your settings files, in the global context you can import external libraries that you'll then be able to use in a function node.

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

Neil Kolban

unread,
Mar 17, 2016, 9:47:12 AM3/17/16
to Node-RED
Thank you Nick.  The documentation is perfect and I apologize for missing that.  I'll be sitting down with a nice coffee later and studying that whole page methodically.  It looks like every words is golden.   I'm afraid I became a "skim reading programmer" and, curmudgeon that I am, I chastise others for that ... and now fell into it myself.   Please consider me suitably ashamed.

Dave C-J

unread,
Mar 17, 2016, 10:02:10 AM3/17/16
to node...@googlegroups.com
While the above is the recommended pattern, there is also the node-red-contrib-npm  node that allows you to use simple npm packages directly - (not all npms are suitable - but there is an example here - http://flows.nodered.org/flow/3c17c7bf227d1a8952b7 )

Luiey

unread,
Mar 20, 2016, 11:21:09 PM3/20/16
to Node-RED
I was too..having thought of that at first using node-RED.. I was writing the "require" at function nodes and when inject, the message contains "require is not defined" things. I already email this issue to node-red support and suddenly I found your call on this group. Seriously having thought of that. Lol! :-D

Julian Knight

unread,
Mar 21, 2016, 5:24:25 AM3/21/16
to Node-RED
There is also a recently create "unsafe function" node that should allow require to be used. Of course, it is called "unsafe" for a reason!
Reply all
Reply to author
Forward
0 new messages