# How to use an npm module within a function node?

**URL:** <https://discourse.nodered.org/t/how-to-use-an-npm-module-within-a-function-node/20917>\
**Category:** General\
**Created:** [27 January 2020 17:47 UTC](https://discourse.nodered.org/t/how-to-use-an-npm-module-within-a-function-node/20917 "2020-01-27T17:47:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![123](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/123/32/2926_2.png) [@123](https://discourse.nodered.org/u/123)\
**Post date:** [27 January 2020 17:47 UTC](https://discourse.nodered.org/t/how-to-use-an-npm-module-within-a-function-node/20917/1 "2020-01-27T17:47:15Z")

</div>

I would like to use this npm module: [https://www.npmjs.com/package/words-to-numbers](https://www.npmjs.com/package/words-to-numbers) within a function node.

Can I install the module using `npm install words-to-numbers` and then refer to it within a function node like this?

```auto
const { wordsToNumbers } = require('words-to-numbers');
msg.numbers = wordsToNumbers(msg.payload); 

```

* * *

What I have done is install [node-red-contrib-npm](https://www.npmjs.com/package/node-red-contrib-npm) and then use that to expose the words-to-numbers module as a custom node. I added the resulting custom node to my flow and it works. However, for my purposes, it would be neater if I could simply refer to it directly within my existing function node.

---

<div class="post-metadata">

**Author:** ![Nxito](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nxito/32/88293_2.png) [@Nxito](https://discourse.nodered.org/u/Nxito)\
**Post date:** [27 January 2020 18:00 UTC](https://discourse.nodered.org/t/how-to-use-an-npm-module-within-a-function-node/20917/2 "2020-01-27T18:00:25Z")

</div>

[Look at this](https://discourse.nodered.org/t/how-to-use-other-js-files/18999/3)

---

<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 January 2020 18:50 UTC](https://discourse.nodered.org/t/how-to-use-an-npm-module-within-a-function-node/20917/3 "2020-01-27T18:50:35Z")

</div>

Also the documentation is worth checking before asking: [https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules](https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules)

---

<div class="post-metadata">

**Author:** ![123](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/123/32/2926_2.png) [@123](https://discourse.nodered.org/u/123)\
**Post date:** [27 January 2020 20:05 UTC](https://discourse.nodered.org/t/how-to-use-an-npm-module-within-a-function-node/20917/4 "2020-01-27T20:05:07Z")

</div>

Well, pardon my thick skull because I read both the linked post and the documentation _before_ circling back here to ask how it's done (to get replies to read what I originally failed to understand). 🤷‍♂️

I understand I need to install the module like this:

```auto
cd ~/.node-red
npm install words-to-numbers

```

My `settings.js` currently contains this:

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

```

I assume it should be changed to this and then restart Node-Red to adopt the change:

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

```

Then in the function node, I need to refer to it like this:

```auto
// declare it
var wordsToNumbers = global.get("wordsToNumbers");
// proceed to use it ...
msg.numbers = wordsToNumbers(msg.payload);

```

Is that correct?

---

<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 January 2020 22:07 UTC](https://discourse.nodered.org/t/how-to-use-an-npm-module-within-a-function-node/20917/5 "2020-01-27T22:07:08Z")

</div>

Hi @123

for the general question of how to import an npm module, you have it all right.

Where your code will hit an error is more specific to the `words-to-numbers` function.

In your Function node, the code:

```auto
var wordsToNumbers = global.get("wordsToNumbers");

```

gets the Object returned by the `require` call. Checking the docs for that module, that Object is not a function you can call directly. Instead, that object has a property called `wordsToNumbers` and _that_ is the function you want to call.

```auto
msg.numbers = wordsToNumbers.wordsToNumbers(msg.payload);

```

Nick

---

<div class="post-metadata">

**Author:** ![123](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/123/32/2926_2.png) [@123](https://discourse.nodered.org/u/123)\
**Post date:** [28 January 2020 02:41 UTC](https://discourse.nodered.org/t/how-to-use-an-npm-module-within-a-function-node/20917/6 "2020-01-28T02:41:11Z")

</div>

Thank you for the confirmation.

> [@knolleary](#):
>
> that object has a property called `wordsToNumbers` and _that_ is the function you want to call.

This became apparent to me when I tried using the word-to-numbers module via the npm node. I had to configure the npm node like this:

![Screenshot from 2020-01-27 15-08-24](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/33db219f5334c2aa94a0f931047de63a744d5f58.png)

However, it would have required a few failed attempts before I correctly translated that into:

```auto
wordsToNumbers.wordsToNumbers(msg.payload);

```

so I appreciate your assistance.

* * *

I've learned a lot from this experience but, ultimately, I chose to follow a different path. I uninstalled the words-to-numbers module and replaced it with a bespoke solution. I only need to translate numbers from zero to one hundred in increments of five. That's easily done with a `map` which involves a lot less overhead than the words-to-numbers module. Plus I can share the flow with a fewer prerequisites (i.e. the installation of the words-to-numbers module).

```auto
// Convert light level from words to numbers
let words = new Map([
	["zero", 0],
	["five", 5],
	["ten", 10],
	["fifteen", 15],
	["twenty", 20],
	["twenty five", 25],
	["thirty", 30],
	["thirty five", 35],
	["forty", 40],
	["forty five", 45],
	["fifty", 50],
	["fifty five", 55],
	["sixty", 60],
	["sixty five", 65],
	["seventy", 70],
	["seventy five", 70],
	["eighty", 80],
	["eighty five", 85],
	["ninety", 90],
	["ninety five", 95],
	["hundred", 100],
	["one hundred", 100],
	]);

var tmp = words.get(msg.payload);
if (tmp) {
       msg.value = tmp;
       return msg;
}
return null;

```

---

<div class="post-metadata">

**Author:** ![sheminasalam](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sheminasalam/32/8981_2.png) [@sheminasalam](https://discourse.nodered.org/u/sheminasalam)\
**Post date:** [11 February 2020 13:16 UTC](https://discourse.nodered.org/t/how-to-use-an-npm-module-within-a-function-node/20917/7 "2020-02-11T13:16:25Z")

</div>

Can anybody post a simple flow using number-to-words npm package? I am running node red in HA.

---

<div class="post-metadata">

**Author:** ![123](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/123/32/2926_2.png) [@123](https://discourse.nodered.org/u/123)\
**Post date:** [11 February 2020 14:55 UTC](https://discourse.nodered.org/t/how-to-use-an-npm-module-within-a-function-node/20917/8 "2020-02-11T14:55:29Z")

</div>

I no longer have the flow containing the words-to-number module. However, the installation process, and using the module, is very easy.

- Install node-red-contrib-npm via the usual way: `Manage Palette > Install`

- After it's installed, find the `npm` node in the left-hand menu and drag and drop it into the flow you are creating.

- Double-click the new npm node and enter the following information:

- The npm node will now download and install the words-to-numbers module (it may take a minute or two).

- When done, the npm node is now a words-to-numbers node. The node's input accepts numeric words (like `fifty five`) and the node's output will be numbers (`55`).

---

<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:** [11 April 2020 14:55 UTC](https://discourse.nodered.org/t/how-to-use-an-npm-module-within-a-function-node/20917/9 "2020-04-11T14:55:40Z")

</div>

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