# Using Moment.js in a function node - NR 3.01 - 2023?

**URL:** https://discourse.nodered.org/t/using-moment-js-in-a-function-node-nr-3-01-2023/76916
**Category:** General
**Created:** [25 March 2023 03:21 UTC](https://discourse.nodered.org/t/using-moment-js-in-a-function-node-nr-3-01-2023/76916 "2023-03-25T03:21:09Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [25 March 2023 03:21 UTC](https://discourse.nodered.org/t/using-moment-js-in-a-function-node-nr-3-01-2023/76916/1 "2023-03-25T03:21:09Z")

</div>

Guys

What is the state of the nation in terms of using Moment.js in a function node as of NR 3.01 and 2023 ?

I believe that Moment.js is now included with NR due to support in the change node/jsonata etc.

What is the method therefore for calling moment in a function node for dat and time formatting ?

Having done a search on this already it appears that the solution has evolved over time and am unsure if i need to install the NPM module now, make the addition to settings.js etc - or if i can happily just declare it at the top of my function and away i go ?

Craig

---

<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: [25 March 2023 08:05 UTC](https://discourse.nodered.org/t/using-moment-js-in-a-function-node-nr-3-01-2023/76916/2 "2023-03-25T08:05:47Z")

</div>

> [@craigcurtin](#):
>
> What is the method therefore for calling moment in a function node for dat and time formatting ?

You have to include it in the **Setup** tab of any Function node you want to use it

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/f/5f619fb25882c50223fe4552d9b39bcf92351714.png)

Example of formatting today's date

```auto
msg.payload = moment().format("YYYY-MM-DD HH:mm:ss")
return msg;

```

* * *

If an npm package is already in `.node-red/node_modules`, possibly because it was installed by another node, Node-red will use that when you add it in a Function's Setup tab .. if its missing it will automatically install it.

In `settings.js` there is the following settings to allow or deny this functionality.

```auto
  /** Allow the Function node to load additional npm modules directly */
  functionExternalModules: true,

```

---

<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: [25 March 2023 12:57 UTC](https://discourse.nodered.org/t/using-moment-js-in-a-function-node-nr-3-01-2023/76916/3 "2023-03-25T12:57:37Z")

</div>

> [@UnborN](#):
>
> You have to include it in the **Setup** tab of any Function node you want to use it

You can also use JSONata in a function node - which already includes Moment of course.

---

<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: [25 March 2023 14:04 UTC](https://discourse.nodered.org/t/using-moment-js-in-a-function-node-nr-3-01-2023/76916/4 "2023-03-25T14:04:39Z")

</div>

I think $moment() was added to jsonata by Node-red but only available in the Change node.  
The jsonata library by itself natively doesnt have it as function.

but even so .. if someone would resort to using a Function node, it means they have some knowledge of JS and i doubt they would be using jsonata which as demonstrated in some posts is a bit slower than plain JS.

---

<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: [25 March 2023 14:41 UTC](https://discourse.nodered.org/t/using-moment-js-in-a-function-node-nr-3-01-2023/76916/5 "2023-03-25T14:41:29Z")

</div>

> [@UnborN](#):
>
> I think $moment() was added to jsonata by Node-red but only available in the Change node.  
> The jsonata library by itself natively doesnt have it as function.

It's OK because it looks like I made up that fact anyway! I was convinced that JSONata had been added as a function to function nodes but I can't find any references to it at all.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [25 March 2023 14:54 UTC](https://discourse.nodered.org/t/using-moment-js-in-a-function-node-nr-3-01-2023/76916/6 "2023-03-25T14:54:11Z")

</div>

Do you mean these?  
[evaluateJSONataExpression](https://nodered.org/docs/api/modules/v/1.3/@node-red_util_util.html#.evaluateJSONataExpression)  
[prepareJSONataExpression](https://nodered.org/docs/api/modules/v/1.3/@node-red_util_util.html#.prepareJSONataExpression)

---

<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: [25 March 2023 15:31 UTC](https://discourse.nodered.org/t/using-moment-js-in-a-function-node-nr-3-01-2023/76916/7 "2023-03-25T15:31:51Z")

</div>

it works .. Interesting, never knew those existed 😉

```auto
const expression = '$moment().format("YYYY-MM-DD HH:mm:ss")';

let exp = RED.util.prepareJSONataExpression(expression, node)

RED.util.evaluateJSONataExpression(exp, msg, (err, res) => {
    node.send({ payload : res})
})

```

---

<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: [25 March 2023 21:00 UTC](https://discourse.nodered.org/t/using-moment-js-in-a-function-node-nr-3-01-2023/76916/8 "2023-03-25T21:00:58Z")

</div>

> [@E1cid](#):
>
> Do you mean these?

Grrr 😁 well found. Added to my Obsidian knowledgebase as it should have been before.

> [@UnborN](#):
>
> never knew those existed

Well hidden indeed.

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [25 March 2023 22:52 UTC](https://discourse.nodered.org/t/using-moment-js-in-a-function-node-nr-3-01-2023/76916/9 "2023-03-25T22:52:32Z")

</div>

thanks for chiming in everyone - hopefully all of that will help out the next person who wants to use moment in a simple fnction node

Much appreciated.

Craig

---

<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: [8 April 2023 22:53 UTC](https://discourse.nodered.org/t/using-moment-js-in-a-function-node-nr-3-01-2023/76916/10 "2023-04-08T22:53:13Z")

</div>

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