How to use external libraries with Triggers and Functions

Last updated 22, Mar 2024

Goal

Explain how to use third-party npm packages when programming Triggers and Functions in JavaScript libraries.

Solution

Create a new working directory for your library. Use npm init -y to create a new package.json.

Install a new package

> npm install mathjs

The dependency will be added in the package.json.

"dependencies": {
    "mathjs": "^11.11.2"
  }

To make use of the external package, we need to import it into our index.js.

#!js name=example api_version=1.0
import {pi} from 'mathjs';

function calculateCircleArea(radius) {
    return pi * radius * radius
};

redis.registerFunction('calculateArea', calculateCircleArea);

To upload this as a single file, use the RedisGears SDK as described in the Triggers and Functions development page.

References