[{"data":1,"prerenderedAt":217},["ShallowReactive",2],{"blog-\u002Fblog\u002F2023\u002F06\u002Fimport-modules":3},{"id":4,"title":5,"body":6,"description":202,"extension":203,"meta":204,"navigation":83,"path":213,"seo":214,"stem":215,"__hash__":216},"blog\u002Fblog\u002F2023\u002F06\u002Fimport-modules.md","Use any npm module in Node-RED",{"type":7,"value":8,"toc":193},"minimark",[9,24,32,37,45,48,74,78,90,106,109,113,118,131,135,140,153,161,167,171,182,185],[10,11,12,13,19,20],"p",{},"Node-RED has ",[14,15,18],"a",{"href":16,"target":17},"https:\u002F\u002Fflows.nodered.org\u002Fsearch?type=node","_blank","an incredibly rich resource of integrations available",", but sometimes you need that little bit of extra functionality, or access to a Node.js module that doesn't have it's own custom nodes in Node-RED. ",[21,22,23],"strong",{},"We can easily import any npm module within the built-in Node-RED function nodes.",[10,25,26,27,31],{},"Historically in Node-RED, you would have needed to manually ",[28,29,30],"code",{},"npm install"," modules from the command line, but now that it's so easy to run Node-RED in the Cloud, where you don't have easy access to those tools, what are the other options available?",[33,34,36],"h2",{"id":35},"function-node-setup","Function Node - Setup",[10,38,39],{},[40,41],"img",{"alt":42,"src":43,"title":44},"Location of the \"add\" button in order to import an npm module intoa  function node","\u002Fblog\u002F2023\u002F06\u002Fimages\u002Fnpmimport-add.jpg","Location of the 'add' button in order to import an npm module intoa  function node",[10,46,47],{},"All you need is the name of the module you want to import, then:",[49,50,51,55,58,61,68],"ol",{},[52,53,54],"li",{},"Drop in a new \"function\" node & double-click it",[52,56,57],{},"Switch to the \"Setup\" tab",[52,59,60],{},"Underneath the \"modules\" tab, click \"+ add\" in the bottom-left of the window.",[52,62,63,64,67],{},"Enter the name of the module you want to use in the newly created row, and (optionally) modify the ",[28,65,66],{},"variable"," that this module will be imported in as.",[52,69,70,71,73],{},"Switch back to the \"On Message\" tab and write your function. Your new module will be available via the ",[28,72,66],{}," you defined in the \"Setup\" tab.",[33,75,77],{"id":76},"example:-moment.js","Example: Moment.js",[79,80,84,85],"video",{"width":81,"height":82,"controls":83},560,315,true,"\n  ",[86,87],"source",{"src":88,"type":89},"https:\u002F\u002Fwebsite-data.s3.eu-west-1.amazonaws.com\u002FMomentJS+Demo.mp4","video\u002Fmp4",[10,91,92,93,99,100,105],{},"Recently we wanted to use ",[14,94,98],{"href":95,"rel":96},"https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fmoment",[97],"nofollow","moment"," for some custom date calculations. Whilst there was set of ",[14,101,104],{"href":102,"rel":103},"https:\u002F\u002Fflows.nodered.org\u002Fnode\u002Fnode-red-contrib-moment",[97],"Moment Node-RED nodes"," already available, it didn't have all of the functionality we needed.",[10,107,108],{},"So, all we needed to do was import the module into a function node, and define our comparison there instead, here's a working example:",[33,110,112],{"id":111},"example:-easy-crc","Example: Easy CRC",[79,114,84,115],{"width":81,"height":82,"controls":83},[86,116],{"src":117,"type":89},"https:\u002F\u002Fwebsite-data.s3.eu-west-1.amazonaws.com\u002FEasy+CRC+Demo.mp4",[10,119,120,121,126,127,130],{},"Something we see ",[14,122,125],{"href":123,"rel":124},"https:\u002F\u002Fdiscourse.nodered.org\u002Fsearch?q=crc%20order%3Alatest",[97],"a lot on the Node-RED Forums"," are questions on how to conduct CRC calculations. There is a popular node module ",[28,128,129],{},"easy-crc"," that can be imported and used in the function nodes, e.g:",[33,132,134],{"id":133},"example:-posthog","Example: PostHog",[79,136,84,137],{"width":81,"height":82,"controls":83},[86,138],{"src":139,"type":89},"https:\u002F\u002Fwebsite-data.s3.eu-west-1.amazonaws.com\u002FPostHog+Node+Demo.mp4",[10,141,142,143,147,148,152],{},"Node-RED is great for ",[14,144,146],{"href":145},"\u002Fsolutions\u002Fdata-integration\u002F","data integration",". We use ",[14,149,151],{"href":150,"target":17},"https:\u002F\u002Fposthog.com\u002F","PostHog"," for our internal Product Analysis. We record live events as they occur on FlowFuse Cloud to better understand features that are (and are not) used.",[10,154,155,156,160],{},"We wanted to investigate whether or not we could add backdated data, which in theory was possible via their ",[14,157,159],{"href":158,"target":17},"https:\u002F\u002Fposthog.com\u002Fdocs\u002Flibraries\u002Fnode","posthog-node"," module. We wanted to populate it with data driven from our own database and API.",[10,162,163,164,166],{},"Within two minutes, we could wire up a node to retrieve data from our API, and then ingest it into ",[28,165,159],{}," via the import of a function node.",[33,168,170],{"id":169},"simplify-function-node-creation-with-flowfuse","Simplify Function Node Creation with FlowFuse",[10,172,173,177,178,181],{},[14,174,176],{"href":175},"\u002F","FlowFuse"," provides a powerful platform to enhance, scale, and secure your Node-RED applications efficiently. One of our latest features, the ",[21,179,180],{},"FlowFuse Assistant",", is designed to streamline the process of creating Function nodes.",[10,183,184],{},"With the FlowFuse Assistant, you can leverage AI to generate Function nodes effortlessly. Just input your prompt, and the Assistant will handle the creation for you, saving time and reducing manual coding.",[10,186,187,188,192],{},"To explore how to make the most of the FlowFuse Assistant and its capabilities, check out the ",[14,189,191],{"href":190},"\u002Fdocs\u002Fuser\u002Fexpert\u002F","Assistants Documentation",".",{"title":194,"searchDepth":195,"depth":195,"links":196},"",2,[197,198,199,200,201],{"id":35,"depth":195,"text":36},{"id":76,"depth":195,"text":77},{"id":111,"depth":195,"text":112},{"id":133,"depth":195,"text":134},{"id":169,"depth":195,"text":170},"Node-RED has an incredibly rich resource of integrations available, but sometimes you need that little bit of extra functionality, or access to a Node.js module that doesn't have it's own custom nodes in Node-RED. We can easily import any npm module within the built-in Node-RED function nodes.","md",{"navTitle":5,"excerpt":205},{"type":7,"value":206},[207],[10,208,12,209,19,211],{},[14,210,18],{"href":16,"target":17},[21,212,23],{},"\u002Fblog\u002F2023\u002F06\u002Fimport-modules",{"title":5,"description":202},"blog\u002F2023\u002F06\u002Fimport-modules","tiDlCnEFhloLMBPZ5jehCaIrSSAGL6dlJ7ZsDL1A5Jo",1780132425615]