【问题标题】:Uncaught Error: Module name "twilio" has not been loaded yet未捕获的错误:尚未加载模块名称“twilio”
【发布时间】:2016-03-01 22:21:03
【问题描述】:

问题

当我希望通过 Twilio 发送预先完成的 SMS 消息时,我在终端中收到与 require 方法相关的错误。我已经阅读了其他类似的 StackOverflow 问题,并尝试在我的脚本部分我的 index.html 中使用包含 RequireJS 的 CDN,或者 npm 安装 Browserify,但我不确定为什么我仍然收到错误。

错误

Uncaught Error: Module name "twilio" has not been loaded yet for context: _. Use require([])

scripts.js

// Twilio Credentials
var accountSid = 'AC7*********';
var authToken = '6b6*********';

// Require the Twilio module and create a REST client
var client = require('twilio')(accountSid, authToken);

client.messages.create({
    to: "+16479933461",
    from: "+12044002143",
    body: "There is a new highest bidder. Visit {{websiteUrl}} to place another bid. All proceeds from the silent auction will go to the Samaritian House.",
    mediaUrl: "http://farm2.static.flickr.com/1075/1404618563_3ed9a44a3a.jpg",
}, function(err, message) {
    console.log(message.sid);
});

【问题讨论】:

    标签: javascript node.js requirejs twilio browserify


    【解决方案1】:

    这里是 Twilio 开发者宣传员。

    根据RequireJS errors page,像这样使用RequireJS时,应该使用异步加载方式,像这样:

    // Twilio Credentials
    var accountSid = 'AC7*********';
    var authToken = '6b6*********';
    
    // Require the Twilio module and create a REST client
    require(['twilio'], function(twilio){
      var client = twilio(accountSid, authToken);
    
      client.messages.create({
          to: "+16479933461",
          from: "+12044002143",
          body: "There is a new highest bidder. Visit {{websiteUrl}} to place another bid. All proceeds from the silent auction will go to the Samaritian House.",
          mediaUrl: "http://farm2.static.flickr.com/1075/1404618563_3ed9a44a3a.jpg",
      }, function(err, message) {
          console.log(message.sid);
      });
    });
    

    如果这是在终端中,我可以问你为什么使用 RequireJS 吗? Node.js 有 require 内置和同步的。

    我想知道您是否尝试在前端使用 Twilio 模块?如果是这样,您不应该公开您的帐户凭据,这可能会被用来滥用您的帐户。在服务器上执行 Twilio API 请求会更好、更安全。这就是 Node.js 模块的用途。

    【讨论】:

    • 仍然试图将我的头脑围绕在 Node 上,类似的措辞,之前在 Stack 上提出的问题指出 RequireJS 作为一种可能的解决方案。我的目的是能够有一个用于无声拍卖的应用程序,通过 SMS 告诉人们有人使用 Twilio 出价超过了他们。现在我想知道您提到了安全性,我应该在哪里存储我的凭据?
    • 它们应该存储在服务器上,你现在是在服务器上做的还是在前端做的?
    • 现在,整个应用都建立在前端。所以我很好奇什么是我最好的选择?
    • 您真的不想在线公开您的凭据。您要做的是构建一个后端服务来处理发送您可以在前端使用 Ajax 方法调用的 SMS 消息。 Twilio 网站上有很多关于如何执行此操作的教程。这可能是一个好的开始:twilio.com/docs/tutorials/walkthrough/lead-alerts/node/express
    • 有什么方法可以解决这个问题而无需构建后端?
    猜你喜欢
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    相关资源
    最近更新 更多