# NodeRed not finding moment.js - "TypeError: moment is not a function"

**URL:** https://discourse.nodered.org/t/nodered-not-finding-moment-js-typeerror-moment-is-not-a-function/94735
**Category:** General
**Created:** [19 January 2025 15:02 UTC](https://discourse.nodered.org/t/nodered-not-finding-moment-js-typeerror-moment-is-not-a-function/94735 "2025-01-19T15:02:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![fabiorcosta](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fabiorcosta/32/98198_2.png) [@fabiorcosta](https://discourse.nodered.org/u/fabiorcosta)
#### Post date: [19 January 2025 15:02 UTC](https://discourse.nodered.org/t/nodered-not-finding-moment-js-typeerror-moment-is-not-a-function/94735/1 "2025-01-19T15:02:43Z")

</div>

I have installed moment through "manage pallete".  
Also added to settings.js

```auto
 functionGlobalContext: {
   moment: require('moment')  
}

```

But when trygin to execute a function node:

```auto
const moment = global.get('moment');  
var tempon = global.get('ultimaON');
var tempoff = global.get('ultimaOFF');
const hoje = moment();
const dataOn = moment(tempon);
const dataOff = moment(tempoff);
const diferencaDiasON = hoje.diff(dataOn, 'days');
const diferencaDiasOFF = hoje.diff(dataOff, 'days');
if (!dataOn.isValid()) {
    node.warn("Data em 'tempon' não é válida.");
}

if (!dataOff.isValid()) {
    node.warn("Data em 'tempoff' não é válida.");
}

return [
    { payload: diferencaDiasON }, // Diferença de dias para tempon
    { payload: diferencaDiasOFF } // Diferença de dias para tempoff
];

```

I am getting "TypeError: moment is not a function"

---

<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 January 2025 15:18 UTC](https://discourse.nodered.org/t/nodered-not-finding-moment-js-typeerror-moment-is-not-a-function/94735/2 "2025-01-19T15:18:21Z")

</div>

> [@fabiorcosta](#):
>
> I have installed moment through "manage pallete"

I assume you mean you have installed a node-red contrib `node` that has the name `moment` in it? like `node-red-contrib-moment` ? That will not give you access to the moment library in a function node!

Why not simply add `moment` through the setup tab on the function? e.g:  
[Use any npm module in Node-RED • FlowFuse](https://flowfuse.com/blog/2023/06/import-modules/#example%3A-moment.js)

Or, why not simply use the `node-red-contrib-moment` node to do this all graphically in the low-code environment?

Or, better still, use built in `$moment` JSONata function in a change node?

---

<div class="post-metadata">

### Author: ![fabiorcosta](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fabiorcosta/32/98198_2.png) [@fabiorcosta](https://discourse.nodered.org/u/fabiorcosta)
#### Post date: [19 January 2025 17:18 UTC](https://discourse.nodered.org/t/nodered-not-finding-moment-js-typeerror-moment-is-not-a-function/94735/3 "2025-01-19T17:18:55Z")

</div>

You assumed right.

I will try the moment contrib node. I am not experienced with json.

I used ChatGPT to generated the code I posted. Thanks a lot

---

<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: [19 April 2025 17:18 UTC](https://discourse.nodered.org/t/nodered-not-finding-moment-js-typeerror-moment-is-not-a-function/94735/4 "2025-04-19T17:18:59Z")

</div>

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