Function node - creating uuid in the code

I am new to node-red and am trying to experiment with following code in a function node:

var ce = {};
ce.id = "a8fd123d-5ac6-44bb-a146-898959a1812e";
msg.payload = ce;
return msg;

Except that I want to generate a new UUID for ce.id property. Not sure how to go about it. When I searched in the forum, there was a post from 2018. They seemed to solve this using hard-coded path for the uuid module. This is 2021. Not sure if there is a better way. I definitely don't want to hard code any paths to the modules.

Any help is appreciated.

Yes, if you have node-red 1.3.x you can enable functionExternalModules in your settings file....

functionExternalModules
if set to true, the Function node’s Setup tab will allow adding additional modules that will become available to the function. See Writing Functions for more information. Default: false.

https://nodered.org/docs/user-guide/writing-functions#using-the-functionexternalmodules-option

Welcome rudaNode,

why are you not using an existing node which provides this functionality?

I did following as you suggested:

  1. Set functionExternalModules = true in settings file.
  2. In Setup tab of the function, set following:
  3. and deployed.

I am getting following error:
19 Jun 11:24:02 - [error] [function:c8620f48.b1bad] Failed to load module : Error: Module not allowed
node_modules/@node-red/util/lib/log.js:94
19 Jun 11:24:02 - [error] [function:c8620f48.b1bad] Error: Function node failed to load external modules

What am I doing wrong?

You cannot currently require modules with a / in them. This is fixed in the next release.

For now, just put uuid and then in your code I think you should be able to do use uuid.v1 (assuming you use uuid as the variable name as well)

That worked!

In function Setup, use following: const myUuid = require(uuid)
In function On Message, used following ce.id = myUuid.v1();

Thank you very much for your help.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.