如何在Twilio功能中使用自定义模块?
我正在寻找一种简化和将某些代码提取到自定义common.js模块中的方法,以使我的Twilio功能更可读。
我期望无服务器的API自动使用自定义JS文件,并让我在需要的地方需要它,但是在部署后,找不到它。
如果有的话,是否有适当的方法,例如:
const utils = require('./libs/utils.js');
exports.handler = async function(context, event, callback) {
...
utils.do_this();
尝试将我带给我:
{"Message":"Cannot find module './libs/utils.js'\nRequire stack:\n- /var/task/handlers/ZN5be18c53f5acf0299a224607fdeccedb.js\n- /var/task/node_modules/runtime-handler/index.js\n- /var/task/runtime-handler.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","name":"Error","stack":"Error: Cannot find module './libs/utils.js'\nRequire stack:\n- /var/task/handlers/ZN5be18c53f5acf0299a224607fdeccedb.js\n- /var/task/node_modules/runtime-handler/index.js\n- /var/task/runtime-handler.js\n- /...
I was looking for a way to simplify and extract some of the code into a custom common.js module to make my Twilio function more readable.
I was expecting the serverless api to take the custom js file automatically and let me require it where I wanted it, but after deploying, it cant be found.
Is there a proper way if any, to do something like:
const utils = require('./libs/utils.js');
exports.handler = async function(context, event, callback) {
...
utils.do_this();
Trying this brings me to:
{"Message":"Cannot find module './libs/utils.js'\nRequire stack:\n- /var/task/handlers/ZN5be18c53f5acf0299a224607fdeccedb.js\n- /var/task/node_modules/runtime-handler/index.js\n- /var/task/runtime-handler.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","name":"Error","stack":"Error: Cannot find module './libs/utils.js'\nRequire stack:\n- /var/task/handlers/ZN5be18c53f5acf0299a224607fdeccedb.js\n- /var/task/node_modules/runtime-handler/index.js\n- /var/task/runtime-handler.js\n- /...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在Twilio的Doc Swamp深处游泳时,我注意到有关于如何执行此操作的提示。
基本上,您必须
即,如果您的文件名是
utils.js
,请将其重命名为utils.private.js
Swimming deep in Twilio's doc swamp, I noticed there is a hint on how to do this.
Basically you MUST
i.e. if your file name is
utils.js
, rename it toutils.private.js