Generation of UUID in function node

I installed node-red-contrib-uuid, primarily to ensure uuid was installed. Then I changed Node Red's settings.js to include the following:

functionGlobalContext: {
    uuidv1:require('uuid/v1')
}

After restarting Node Red, I set up a function node containing the following:

    var uuidv1 = global.get('uuidv1');

However, on running, it is null. Shouldn't this work? Or is there a better way?

I installed the node just for testing. As expected new node appears in the palette. I just use it as a regular node. No need to modify settings.js. I woud rather use a change node (instead of a function node) to set the global variable.

r-02

r-001

Flow:

[{"id":"c1f16100.8a209","type":"tab","label":"Flow 6","disabled":false,"info":""},{"id":"c058e8c7.805648","type":"uuid","z":"c1f16100.8a209","uuidVersion":"v4","namespaceType":"","namespace":"","namespaceCustom":"","name":"","field":"payload","fieldType":"msg","x":390,"y":200,"wires":[["934b85ea.250b08"]]},{"id":"71aa6ce4.c1f2b4","type":"debug","z":"c1f16100.8a209","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":750,"y":200,"wires":[]},{"id":"6b757a91.c4efc4","type":"inject","z":"c1f16100.8a209","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":200,"wires":[["c058e8c7.805648"]]},{"id":"934b85ea.250b08","type":"change","z":"c1f16100.8a209","name":"","rules":[{"t":"set","p":"uuidv1","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":200,"wires":[["71aa6ce4.c1f2b4"]]}]
1 Like

I was wondering that perhaps what you want is not using the UUID node (which would be trivial) but instead use the module uuid (that you installed as an NPM module) inside a function node.

I tried now:
1- Installed UUID module (npm install uuid)
2- Patched the settings.js file in node-RED

    functionGlobalContext: {
        //uuid: "3d0ca315-aff9–4fc2-be61–3b76b9a2d798",
        jsonata:require('jsonata'),
        uuidv1:require('uuid/v1')

to find that uuid is not working inside the function node.

As far as I know, the uuid module was supposed to appear in the context sidebar (since it wass added in the functionGlobalContext) but this is not happening so I suspect there is an issue with the module.

Investigating further I noticed that there was already an uuid module installed in my computer. Apparently, that one was installed by Microsoft.

I have to revise what is the order that Node.JS respect when you invoke require() and find how to force the correct one to be used.

now it is ok. Forced the full path in the settings.js file

    functionGlobalContext: {
        //uuid: "3d0ca315-aff9–4fc2-be61–3b76b9a2d798",
        jsonata:require('jsonata'),
        uuidv:require('C:/Users/OCM/node_modules/uuid/v1')

Code inside the function:

var j = global.get('uuidv'); 
msg.payload = j();
return msg;

s-2

s-3

Flow:

[{"id":"78567e54.59664","type":"tab","label":"Flow 7","disabled":false,"info":""},{"id":"1adab843.5681d8","type":"inject","z":"78567e54.59664","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":200,"y":160,"wires":[["adee4257.ffcc5"]]},{"id":"adee4257.ffcc5","type":"function","z":"78567e54.59664","name":"test UUID","func":"var j = global.get('uuidv'); \nmsg.payload = j();\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":160,"wires":[["13e33b2c.5d4575"]]},{"id":"13e33b2c.5d4575","type":"debug","z":"78567e54.59664","name":"","active":true,"console":false,"complete":"true","x":590,"y":160,"wires":[]}]

UPDATE: Woohoo! That worked. Thanks again.

Thanks, I'll try the full path option. I haven't installed it using npm install uuid because I'm running Node Red as a hass.io add-on under docker and it's (1) locked down a fair bit and (2) if I recreate the container I wouldn't remember to re-install. These are probably poor reasons, but I'm still finding my feet with the stack. I thought I could install the uuid node add-on within Node Red, and it would handle the install of uuid. And yes, as you say, I want to create UUIDs inside a function node because I need a few created each time, and multiple uuid nodes would be a pain, particularly given I need a function node anyway.

1 Like

Good ! Perhaps you could edit/change the post category from "development" to "general" (as this would be more adequate) ?

I see somebody has changed the category, so I'm now educated. After using SO for a while, it's a struggle to use a forum again. It seems very retro, but it seems to be where most of the activity is.