twilio-node: TypeError: url.URL is not a constructor

Issue Summary

I am using latest version of this library 3.44.0. (I see from the npm version log. it’s just a day ago!) and having the following error. TypeError: url.URL is not a constructor when I try to send message. the code is copied from doc. https://www.twilio.com/docs/sms/quickstart/node

Steps to Reproduce

  1. set up simple react project with create-react-app
  2. npm install twilio
  3. execute following code App.js

Code Snippet

const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const authToken = 'your_auth_token';
const client = require('twilio')(accountSid, authToken);

client.messages
  .create({
     body: 'This is the ship that made the Kessel Run in fourteen parsecs?',
     from: '+15017122661',
     to: '+15558675310'
   })
  .then(message => console.log(message.sid));

Exception/Log

# paste exception/log here

image

image

Technical details:

  • twilio-node version: 3.44.0
  • node version: 12.13.0

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 9
  • Comments: 16 (2 by maintainers)

Most upvoted comments

I think there are significant valid use cases wherein someone might want to run twillio-node on a react frontend. For example, in an electron app where the user is also the owner of the twillio account (or are from the same organisation). This is my use case. It seems that, apart from this URL class issue, nothing else would stop twillio to work on the frontend. Is there any workaround ? How could one get access to the URL class native to node, but in a browser?

You can use the REST HTTP API directly using axios for http requests, and qs.stringify to get the parameters encoded properly:

await(axios.post("https://api.twilio.com/2010-04-01/Accounts/" + sid + "/Messages.json", qs.stringify({
  Body: message,
  From: from,
  To: to
}), {
  auth: {
    username: sid,
    password: token
  }
}));

After some digging and help, looks like this is related to https://github.com/defunctzombie/node-url/issues/37

At a high-level, this repo should not be used in a client-side (React) app. Communicating with Twilio APIs requires auth which would be exposed to the end-user. Blog about this and how to re-architect: https://www.twilio.com/blog/send-an-sms-react-twilio

@BannyT

am having the same issue with angular, any one with a satisfactory solution

I found an ugly workaround, hope it helps

URL = typeof window !== 'undefined' && window.URL ? window.URL : URL;

Only once, before any usage of any intercept / request or any url dependencies related.