Share JavaScript code between Twilio functions

201 views Asked by At

I have a Twilio serverless app that contains several functions. Several of the functions have code that is similar that I have extracted out into a separate file that can be used by the loaded and used by the functions.

This works if I run things locally with twilio serverless:start, but fails when I deploy to Twilio and try using the endpoints from their. On Twilio the functions fail with the message Cannot find module '<path to module>' \nRequire stack ...

functions (two files like this):

const share = require('shared-code');

shared-code.js:

exports.helperFn = function() {}

How can I easily share JavaScript code between Twilio functions?

1

There are 1 answers

2
lizziepika On BEST ANSWER

Twilio developer evangelist here.

You can find the path of a Function here. Then use that path in the file in which you want to reference code from another file:

let path = Runtime.getFunctions()['function-path'].path; //example: Runtime.getFunctions()['api/identity'].path;

where the Function path is the Function name after the / in the URL.

Then to use that code from a Function in a different Function, you could require this

let module = require(path);

Let me know if this helps at all!