# 'require' not defined?

**URL:** <https://discourse.nodered.org/t/require-not-defined/94683>\
**Category:** General\
**Created:** [16 January 2025 19:03 UTC](https://discourse.nodered.org/t/require-not-defined/94683 "2025-01-16T19:03:58Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gNeandr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gneandr/32/73947_2.png) [@gNeandr](https://discourse.nodered.org/u/gNeandr)\
**Post date:** [16 January 2025 19:03 UTC](https://discourse.nodered.org/t/require-not-defined/94683/1 "2025-01-16T19:03:58Z")

</div>

Need help with using a module like 'moment' which fails with my setup.  
This is the code added to a function node:

```auto
const moment = require('moment');
const now = moment();
// Format the timestamp
msg.payload = now.format('YYYY-MM-DD HH:mm:ss');

```

It results with an error:  
`ReferenceError: ReferenceError: require is not defined (line 21, col 16)` see more details below

I found `functionExternalModules` needs to be enabled in `settings.js`, it needs to restart NR. All done, no change --\> Error  
I run 'moment' directly from CLI, all good.  
No further ideas here! What could be the problem?

* * *

_(Node : v20.18.1 / npm : 10.8.2)_

```auto
ReferenceError: require is not defined
    at Function node:b54bf4877315a563 [open3e/ecu LISTEN --> FLOW]:22:16
    at Function node:b54bf4877315a563 [open3e/ecu LISTEN --> FLOW]:106:3
    at Script.runInContext (node:vm:149:12)
    at processMessage (/usr/lib/node_modules/node-red/node_modules/@node-red/nodes/core/function/10-function.js:419:33)
    at FunctionNode._inputCallback (/usr/lib/node_modules/node-red/node_modules/@node-red/nodes/core/function/10-function.js:342:17)
    at /usr/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/nodes/Node.js:214:26
    at Object.trigger (/usr/lib/node_modules/node-red/node_modules/@node-red/util/lib/hooks.js:166:13)
    at Node._emitInput (/usr/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/nodes/Node.js:206:11)
    at Node.emit (/usr/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/nodes/Node.js:190:25)
    at Node.receive (/usr/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/nodes/Node.js:499:10)
16 Jan 19:50:21 - [error] [function:open3e/ecu LISTEN --> FLOW] ReferenceError: ReferenceError: require is not defined (line 21, col 16)

```

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [16 January 2025 19:20 UTC](https://discourse.nodered.org/t/require-not-defined/94683/2 "2025-01-16T19:20:31Z")

</div>

> [@gNeandr](#):
>
> `const moment = require('moment');`

you cant use `require` in a Function  
you have to go in the **Setup** tab of the Function node to load the module.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/0/d0a2103ac490cea4417e7d13d72f15be375333fb.png)

and actually it auto-installs .. no need to manually install the module

Then you can use it with whatever name you gave it in the **Import as** section

Test Flow

```auto
[{"id":"da4e78e477349636","type":"function","z":"54efb553244c241f","name":"use moment","func":"msg.payload = moment().format(\"DD MMM YYYY HH:mm\")\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"moment","module":"moment"}],"x":390,"y":3180,"wires":[["afb950181a147d88"]]},{"id":"cc02d872c7c5b82d","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":3180,"wires":[["da4e78e477349636"]]},{"id":"afb950181a147d88","type":"debug","z":"54efb553244c241f","name":"debug 12","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":3180,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [16 January 2025 19:24 UTC](https://discourse.nodered.org/t/require-not-defined/94683/3 "2025-01-16T19:24:41Z")

</div>

Or just use a `change` node 😃  
no need to install `moment` - its built into the `change` node

 ![Screenshot 2025-01-16 at 19.23.45](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/83039cd78857864c6aabcb7c1214f79fd275f2cf.png)

 ![Screenshot 2025-01-16 at 19.24.20](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cbd428715dab52a0088a5374815ad499fc4315bc.png)

```auto
[{"id":"8bab5339272e69b1","type":"change","z":"2d7bf6e3.84c97a","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment().tz('Europe/London').format('YYYY-MM-DD HH:mm:ss')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":1540,"wires":[["7d690285bb0b77aa"]]},{"id":"d828b59fc710a363","type":"inject","z":"2d7bf6e3.84c97a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":280,"y":1540,"wires":[["8bab5339272e69b1"]]},{"id":"7d690285bb0b77aa","type":"debug","z":"2d7bf6e3.84c97a","name":"debug 389","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":1540,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [16 January 2025 19:32 UTC](https://discourse.nodered.org/t/require-not-defined/94683/4 "2025-01-16T19:32:03Z")

</div>

There's also a guide & video which should help you - [https://flowfuse.com/blog/2023/06/import-modules/](https://flowfuse.com/blog/2023/06/import-modules/)

---

<div class="post-metadata">

**Author:** ![gNeandr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gneandr/32/73947_2.png) [@gNeandr](https://discourse.nodered.org/u/gNeandr)\
**Post date:** [16 January 2025 22:31 UTC](https://discourse.nodered.org/t/require-not-defined/94683/5 "2025-01-16T22:31:53Z")

</div>

_This brings me to the following question:_  
Could javascript scripts that are needed at multiple function nodes in the project be written as “modules” so that they don't have to be loaded multiple times. This would also make the creation of the function nodes simpler and clearer.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [16 January 2025 22:43 UTC](https://discourse.nodered.org/t/require-not-defined/94683/6 "2025-01-16T22:43:59Z")

</div>

Sure .. it should work.  
You may have to use `functionGlobalContext` though to load your module  
instead of the Setup tab `functionExternalModules`

`settings.js` section :

```json
/** The following property can be used to set predefined values in Global Context.
     * This allows extra node modules to be made available with in Function node.
     * For example, the following:
     * functionGlobalContext: { os:require('os') }
     * will allow the `os` module to be accessed in a Function node using:
     * global.get("os")
     */
    functionGlobalContext: {
        // os:require('os'),
    },

```

---

<div class="post-metadata">

**Author:** ![gNeandr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gneandr/32/73947_2.png) [@gNeandr](https://discourse.nodered.org/u/gNeandr)\
**Post date:** [16 January 2025 22:49 UTC](https://discourse.nodered.org/t/require-not-defined/94683/7 "2025-01-16T22:49:21Z")

</div>

OK!  
Do you see special requirements for writing such a module?  
And I understood, they have to be loaded in the base directory of .node-red.  
Or ... not 'loaded' but installed with node.js, right?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [16 January 2025 23:10 UTC](https://discourse.nodered.org/t/require-not-defined/94683/8 "2025-01-16T23:10:15Z")

</div>

i think the code should be in CommonJS syntax .. but im not sure.  
Did a small test and this works

Create a `utils.js` file in `./node-red` folder

```auto
const utils = {
  add(a, b) {
    return a + b;
  },
  subtract(a, b) {
    return a - b;
  },
};

module.exports = utils;

```

In settings.js require it

```json
functionGlobalContext: {
    // os:require('os'),
    utils: require("./utils.js"),
  },

```

use it in Function node

```auto
const utils = global.get("utils")

msg.payload = utils.add(5, 2)

return msg

```

---

<div class="post-metadata">

**Author:** ![gNeandr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gneandr/32/73947_2.png) [@gNeandr](https://discourse.nodered.org/u/gNeandr)\
**Post date:** [16 January 2025 23:12 UTC](https://discourse.nodered.org/t/require-not-defined/94683/9 "2025-01-16T23:12:58Z")

</div>

GREAT help! ☺  
Will work with with the next project details. Thanks!

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [16 January 2025 23:16 UTC](https://discourse.nodered.org/t/require-not-defined/94683/10 "2025-01-16T23:16:40Z")

</div>

By the way .. i read on the forum that some people store some helpful Functions in Context ..  
you could use an Inject node to create those on flow load and call them in a similar fashion (global.get("myFunctions"))

---

<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:** [30 January 2025 23:16 UTC](https://discourse.nodered.org/t/require-not-defined/94683/11 "2025-01-30T23:16:47Z")

</div>

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