如何在Twilio功能中使用自定义模块?

发布于 2025-02-08 14:39:50 字数 841 浏览 2 评论 0原文

我正在寻找一种简化和将某些代码提取到自定义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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋意浓 2025-02-15 14:39:50

在Twilio的Doc Swamp深处游泳时,我注意到有关于如何执行此操作的提示

基本上,您必须

  1. 将.js文件添加为私人资产< - 很重要! *无保护的工作!
    即,如果您的文件名是utils.js,请将其重命名为utils.private.js
  2. 以以下方式获取该资产的路径:
// notice you don't need to write .private.
const path = Runtime.getAssets()['/utils.js'].path; 
  1. 需要处理程序内部的路径:
exports.handler = async function(context, event, callback) {
     const path = Runtime.getAssets()['/utils.js'].path; 
     const utils = require(path);

Swimming deep in Twilio's doc swamp, I noticed there is a hint on how to do this.

Basically you MUST

  1. Add the .js file as an PRIVATE ASSET <- IMPORTANT! *unprotected wont work!
    i.e. if your file name is utils.js, rename it to utils.private.js
  2. Get the path of that asset in the following way:
// notice you don't need to write .private.
const path = Runtime.getAssets()['/utils.js'].path; 
  1. Require the path inside the handler:
exports.handler = async function(context, event, callback) {
     const path = Runtime.getAssets()['/utils.js'].path; 
     const utils = require(path);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文