未捕获错误:模块名称“”;twilio”;尚未加载

Uncaught Error: Module name "twilio" has not been loaded yet

本文关键字:twilio 加载 错误 模块      更新时间:2023-09-26

问题

当我想通过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);
});

这里是Twilio开发人员的传道者。

根据RequireJS错误页面,当像这样使用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模块的作用。