6

I've installed a js library https://www.npmjs.com/package/fft with npm, how can I make this available in node-red functions ?

OpenStove
  • 714
  • 1
  • 11
  • 22

1 Answers1

11

This is covered Writing Functions sections of the Node-RED docs

You need to add npm modules to the settings.js file. You can find this file in ~/.node-red/

The section you are looking for is the functionGlobalContext section.

...
functionGlobalContext: {
   fft: require('fft')
},
...

You would then access the module in the function node with the following:

var FFT = context.global.get('fft');
var fft = new FFT(n, inverse);
...

Also be careful where you installed the fft module, it needs to be either in ~/.node-red/node_modules or installed globally so it is accessible to Node-RED.

EDIT:

More recent versions of Node-RED (v1.3.0 onward) have support for loading modules directly in the function node. The docs have been updated to cover this.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • Thank you, I had already try this, but getting the error : Error loading settings file: /home/pi/.node-red/settings.js Error: Cannot find module 'ftt' npm installed ftt in .node-red/node_modules.... I also tried to copy it to node-red/, but still the same problem... – OpenStove Jul 20 '16 at 12:57
  • Edit your original question to include the error (and always include **ALL** the information when you ask the question) – hardillb Jul 20 '16 at 12:58
  • You have a typo in settings.js - the node is called `fft` and you have `ftt` – hardillb Jul 20 '16 at 13:01