Andrew Nguyen
Andrew Nguyen

Reputation: 1436

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

Problem

I'm receiving an error in the terminal related to the require method when I'm looking to send a pre-done SMS message through Twilio. I've read other similar StackOverflow questions and I've tried to use include the CDN for RequireJS in my scripts section my index.html, alternatively npm installing Browserify, but I'm not sure why I'm still getting the error.

Error

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);
});

Upvotes: 0

Views: 1015

Answers (1)

philnash
philnash

Reputation: 73029

Twilio developer evangelist here.

According to the RequireJS errors page, when using RequireJS like this, you should use the asynchronous loading method, like this:

// 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);
  });
});

If this is in the terminal, can I ask why you are using RequireJS though? Node.js has require built in and synchronous.

I'm wondering if you are trying to use the Twilio module in the front end? If so, you shouldn't as you expose your account credentials which could be used to abuse your account. It's better and safer to perform Twilio API requests on a server. That's what the Node.js module is for.

Upvotes: 1

Related Questions