# Function node - creating uuid in the code

**URL:** <https://discourse.nodered.org/t/function-node-creating-uuid-in-the-code/47358>\
**Category:** General\
**Created:** [19 June 2021 06:33 UTC](https://discourse.nodered.org/t/function-node-creating-uuid-in-the-code/47358 "2021-06-19T06:33:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rudaNode](https://avatars.discourse-cdn.com/v4/letter/r/58f4c7/32.png) [@rudaNode](https://discourse.nodered.org/u/rudaNode)\
**Post date:** [19 June 2021 06:33 UTC](https://discourse.nodered.org/t/function-node-creating-uuid-in-the-code/47358/1 "2021-06-19T06:33:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 June 2021 07:09 UTC](https://discourse.nodered.org/t/function-node-creating-uuid-in-the-code/47358/2 "2021-06-19T07:09:51Z")

</div>

> [@rudaNode](#):
>
> They seemed to solve this using hard-coded path for the uuid module. This is 2021. Not sure if there is a better way

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](https://nodered.org/docs/user-guide/writing-functions#using-the-functionexternalmodules-option)

---

<div class="post-metadata">

**Author:** ![H3llsing](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/h3llsing/32/40624_2.png) [@H3llsing](https://discourse.nodered.org/u/H3llsing)\
**Post date:** [19 June 2021 11:32 UTC](https://discourse.nodered.org/t/function-node-creating-uuid-in-the-code/47358/3 "2021-06-19T11:32:28Z")

</div>

Welcome rudaNode,

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

> **[node-red-contrib-uuid](https://flows.nodered.org/node/node-red-contrib-uuid)**
>
> A Node-RED Node for simple, fast generation of RFC4122 UUIDS

---

<div class="post-metadata">

**Author:** ![rudaNode](https://avatars.discourse-cdn.com/v4/letter/r/58f4c7/32.png) [@rudaNode](https://discourse.nodered.org/u/rudaNode)\
**Post date:** [19 June 2021 18:31 UTC](https://discourse.nodered.org/t/function-node-creating-uuid-in-the-code/47358/4 "2021-06-19T18:31:15Z")

</div>

I did following as you suggested:

1. Set functionExternalModules = true in settings file.
2. In Setup tab of the function, set following:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/b/eb9cd10850ba8a65972aa77849c375fbabeee636.png)
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?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [19 June 2021 18:44 UTC](https://discourse.nodered.org/t/function-node-creating-uuid-in-the-code/47358/5 "2021-06-19T18:44:56Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![rudaNode](https://avatars.discourse-cdn.com/v4/letter/r/58f4c7/32.png) [@rudaNode](https://discourse.nodered.org/u/rudaNode)\
**Post date:** [19 June 2021 19:10 UTC](https://discourse.nodered.org/t/function-node-creating-uuid-in-the-code/47358/6 "2021-06-19T19:10:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [18 August 2021 19:10 UTC](https://discourse.nodered.org/t/function-node-creating-uuid-in-the-code/47358/7 "2021-08-18T19:10:37Z")

</div>

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