npmnode-red

Use an external JavaScript library in a Node-RED function


I've installed a JavaScript library, fft.js, with npm. How can I make this available in Node-RED functions?


Solution

  • This is covered Writing Functions sections of the Node-RED documentation.

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

    You are looking for the section functionGlobalContext.

    ...
    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 folder ~/.node-red/node_modules or installed globally, so it is accessible to Node-RED.

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