How to use an npm module within a function node?

I would like to use this npm module: 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?

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

What I have done is install 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.

Look at this

1 Like

Also the documentation is worth checking before asking:

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). :man_shrugging:

I understand I need to install the module like this:

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

My settings.js currently contains this:

    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:

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

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

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

Is that correct?

1 Like

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:

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.

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



Thank you for the confirmation.

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

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


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).

// 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;

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

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).

1 Like

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