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?
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:
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 me know if this helps at all!