# Issue Accessing Custom Global Function in Node-RED Function Node

**URL:** <https://discourse.nodered.org/t/issue-accessing-custom-global-function-in-node-red-function-node/94367>\
**Category:** Dashboard\
**Tags:** dashboard-2\
**Created:** [5 January 2025 09:50 UTC](https://discourse.nodered.org/t/issue-accessing-custom-global-function-in-node-red-function-node/94367 "2025-01-05T09:50:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Subavarsha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/subavarsha/32/92880_2.png) [@Subavarsha](https://discourse.nodered.org/u/Subavarsha)\
**Post date:** [5 January 2025 09:50 UTC](https://discourse.nodered.org/t/issue-accessing-custom-global-function-in-node-red-function-node/94367/1 "2025-01-05T09:50:53Z")

</div>

Hi  
I am trying to define a custom global function in ui\_base.js and access it within a Function node in Node-RED. However, I am encountering an undefined error when attempting to use the function.

```auto
module.exports = function (RED) {
    // Register the global function when the nodes have started
    RED.events.on('nodes-started', () => {
        const globalContext = RED.runtime.context.global;
        globalContext.set('myUtilityFunction', (param) => {
            return `Processed: ${param}`;
        });
    });

    // Custom node definition here, if needed
};

```

Could someone help me identify what might be causing the `undefined` error or how to correctly set up and access the global function in Node-RED?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [5 January 2025 12:28 UTC](https://discourse.nodered.org/t/issue-accessing-custom-global-function-in-node-red-function-node/94367/2 "2025-01-05T12:28:58Z")

</div>

> [@Subavarsha](#):
>
> ui\_base.js

Not sure why you are hacking on that D2 config node?

Would probably be better to actually explain what you are trying to achieve.

Function nodes run code in a protected VM so it is perfectly possible that trying to do the global in that config node might not reach the VM - but I don't know the internals of D2 so not sure.

---

<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:** [4 February 2025 12:29 UTC](https://discourse.nodered.org/t/issue-accessing-custom-global-function-in-node-red-function-node/94367/3 "2025-02-04T12:29:19Z")

</div>

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