Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: url.URL is not a constructor #577

Closed
webdeveloper001 opened this issue May 28, 2020 · 16 comments
Closed

TypeError: url.URL is not a constructor #577

webdeveloper001 opened this issue May 28, 2020 · 16 comments
Labels
status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library

Comments

@webdeveloper001
Copy link

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
@childish-sambino
Copy link
Contributor

Same as #575

The URL class has been available since Node v6.13.0 (reference). Are you sure you're using Node v12?

@childish-sambino childish-sambino added status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library labels Jun 8, 2020
@PedroFabrino
Copy link

PedroFabrino commented Jun 17, 2020

I have the same exact issue, with the twilio video create room method

image

Error:
image

Technical details:
"twilio": "^3.46.0",
node version: v13.12.0

Anyone can provide any tips on how to fix this?

Thank ya'll!

@childish-sambino
Copy link
Contributor

After some digging and help, looks like this is related to defunctzombie/node-url#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

@Zvezdin
Copy link

Zvezdin commented Feb 11, 2021

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?

@mikeymckay
Copy link

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

@BannyT
Copy link

BannyT commented May 19, 2021

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

@mikeymckay
Copy link

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

Did you see my approach above? It's how you make requests without needing to use the twilio-node library.

@BannyT
Copy link

BannyT commented May 19, 2021

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

May you share the whole script so as to easily follow up with your code

@mikeymckay
Copy link

May you share the whole script so as to easily follow up with your code

The whole thing is there. message is a string of what you want to send. from is the twilio number you are using. to is the destination number. sid and token are the twilio authentication details. You will need to import axios and import qs:

axios = require('axios')
qs = require('qs')

This lets you send SMS via Twilio from a browser. It's live in Uganda right now. ;-)

@BannyT
Copy link

BannyT commented May 19, 2021 via email

@mikeymckay
Copy link

mikeymckay commented May 19, 2021 via email

@BannyT
Copy link

BannyT commented May 19, 2021 via email

@BannyT
Copy link

BannyT commented May 19, 2021 via email

@mikeymckay
Copy link

mikeymckay commented May 19, 2021 via email

@Rapiiidooo
Copy link

@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.

@hiepxanh
Copy link

tested with angular 14 SSR, you should lazy load it to prevent it leak to override global namespace URL of nodejs (mention here muaz-khan/RecordRTC#666)

        let z;
        let moduleTw;
        await this.ngZone.run(async () => {
          // https://stackoverflow.com/a/58859327/5748537
          moduleTw = await import('twilio');
          z = (moduleTw as any).default;
        });

   

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library
Projects
None yet
Development

No branches or pull requests

8 participants