# Using moment in function node

**URL:** <https://discourse.nodered.org/t/using-moment-in-function-node/25651>\
**Category:** General\
**Created:** [27 April 2020 21:38 UTC](https://discourse.nodered.org/t/using-moment-in-function-node/25651 "2020-04-27T21:38:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [27 April 2020 21:38 UTC](https://discourse.nodered.org/t/using-moment-in-function-node/25651/1 "2020-04-27T21:38:33Z")

</div>

Hi All,

I wanted to use moment in a function node. I stumbled upon this [issue](https://github.com/node-red/node-red/issues/41) which appears rather old.

I tried using the tip to add the moments to the functionGlobalContext but did not work. I did not get the error during NR startup that it does not find moments, but global.get("moment") did not return anything.

Now, I am using the function-npm node where I can use require and it works:

```auto
var moment = require('moment');

msg.payload = moment().format('MMMM Do YYYY, h:mm:ss a'); // April 27th 2020, 6:08:20 pm
return msg;

```

Is this still the recommended method, or did I missed some other and supported option?

Regards,  
Csongor

---

<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:** [27 April 2020 21:59 UTC](https://discourse.nodered.org/t/using-moment-in-function-node/25651/2 "2020-04-27T21:59:25Z")

</div>

> [@nygma2004](#):
>
> I tried using the tip to add the moments to the functionGlobalContext but did not work.

The recommended method is still what it says in the docs - [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules)

Can you share exactly what you added to your `functionGlobalContext` ?

---

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [27 April 2020 22:05 UTC](https://discourse.nodered.org/t/using-moment-in-function-node/25651/3 "2020-04-27T22:05:39Z")

</div>

This is what it says exactly:

```auto
    functionGlobalContext: {
	moment:require('moment')
        // os:require('os'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
    },

```

I know you mentioned before that moment could be installed in the incorrect directory. I am running NR on Windows now, and it is installed here: C:\Users\xxxxx\AppData\Roaming\npm\node\_modules\node-red\node\_modules\moment.

---

<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:** [27 April 2020 22:07 UTC](https://discourse.nodered.org/t/using-moment-in-function-node/25651/4 "2020-04-27T22:07:22Z")

</div>

You should run the `npm install moment` command in the same directory as your `settings.js` file

---

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [28 April 2020 06:42 UTC](https://discourse.nodered.org/t/using-moment-in-function-node/25651/5 "2020-04-28T06:42:26Z")

</div>

I have done that now:

```auto
C:\Users\xxxxxx\AppData\Roaming\npm\node_modules\node-red>npm install moment
npm notice created a lockfile as package-lock.json. You should commit this file.
+ moment@2.24.0
updated 1 package and audited 1345 packages in 2.51s

1 package is looking for funding
  run `npm fund` for details

found 0 vulnerabilities

```

It is still not working, moment is undefined in the function node. Should I see something in the log when I start node-red? I mean I am not seeing any info or warning that the moment is being loaded or failed to load.

---

<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:** [28 April 2020 06:53 UTC](https://discourse.nodered.org/t/using-moment-in-function-node/25651/6 "2020-04-28T06:53:32Z")

</div>

That suggests you have edited the wrong settings file. Node-RED logs the full path to the settings file it is using when it starts. Make sure you have the right one.

---

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [28 April 2020 06:54 UTC](https://discourse.nodered.org/t/using-moment-in-function-node/25651/7 "2020-04-28T06:54:18Z")

</div>

Sorry for all this, of course I was looking in a wrong directory.  
I have re-installed `npm install moment` in `c:\users\xxxxx\.node-red` where my actual settings.js file as and now everything works. The code in my first comment works in a regular function node.

---

<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:** [12 May 2020 06:54 UTC](https://discourse.nodered.org/t/using-moment-in-function-node/25651/8 "2020-05-12T06:54:19Z")

</div>

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