twilio / twilio-node

Node.js helper library
MIT License
1.39k stars 507 forks source link

TypeError: url.URL is not a constructor #577

Closed webdeveloper001 closed 4 years ago

webdeveloper001 commented 4 years ago

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:

childish-sambino commented 4 years ago

Same as https://github.com/twilio/twilio-node/issues/575

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

PedroFabrino commented 4 years ago

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 commented 4 years ago

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

Zvezdin commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

mikeymckay commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

The challenge am having is that the api recognizes my phone number as invalid even when I use E.164 format On Wed, May 19, 2021 at 10:43 PM Mike McKay @.***> wrote:

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/twilio/twilio-node/issues/577#issuecomment-844414742, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALX4NJRBPOQ45LN7JKUPQHLTOQIHRANCNFSM4NNJP7ZQ .

mikeymckay commented 3 years ago

What's the error code from Twilio?

On Wed, May 19, 2021 at 9:47 PM Tumusiime Barnabus @.***> wrote:

The challenge am having is that the api recognizes my phone number as invalid even when I use E.164 format On Wed, May 19, 2021 at 10:43 PM Mike McKay @.***> wrote:

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub <https://github.com/twilio/twilio-node/issues/577#issuecomment-844414742 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/ALX4NJRBPOQ45LN7JKUPQHLTOQIHRANCNFSM4NNJP7ZQ

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/twilio/twilio-node/issues/577#issuecomment-844417211, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAC5342JT4XMIFR66JKMKDTOQIUFANCNFSM4NNJP7ZQ .

BannyT commented 3 years ago

21211

On Wed, May 19, 2021 at 10:49 PM Mike McKay @.***> wrote:

What's the error code from Twilio?

On Wed, May 19, 2021 at 9:47 PM Tumusiime Barnabus @.***> wrote:

The challenge am having is that the api recognizes my phone number as invalid even when I use E.164 format On Wed, May 19, 2021 at 10:43 PM Mike McKay @.***> wrote:

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub < https://github.com/twilio/twilio-node/issues/577#issuecomment-844414742 , or unsubscribe <

https://github.com/notifications/unsubscribe-auth/ALX4NJRBPOQ45LN7JKUPQHLTOQIHRANCNFSM4NNJP7ZQ

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub <https://github.com/twilio/twilio-node/issues/577#issuecomment-844417211 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/AAAC5342JT4XMIFR66JKMKDTOQIUFANCNFSM4NNJP7ZQ

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/twilio/twilio-node/issues/577#issuecomment-844418746, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALX4NJQBSY2LPWP4OPF4QWTTOQI5BANCNFSM4NNJP7ZQ .

BannyT commented 3 years ago

Any suggestions?

On Wed, May 19, 2021 at 10:53 PM Tumusiime Barnabus < @.***> wrote:

21211

On Wed, May 19, 2021 at 10:49 PM Mike McKay @.***> wrote:

What's the error code from Twilio?

On Wed, May 19, 2021 at 9:47 PM Tumusiime Barnabus @.***> wrote:

The challenge am having is that the api recognizes my phone number as invalid even when I use E.164 format On Wed, May 19, 2021 at 10:43 PM Mike McKay @.***> wrote:

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub < https://github.com/twilio/twilio-node/issues/577#issuecomment-844414742 , or unsubscribe <

https://github.com/notifications/unsubscribe-auth/ALX4NJRBPOQ45LN7JKUPQHLTOQIHRANCNFSM4NNJP7ZQ

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub < https://github.com/twilio/twilio-node/issues/577#issuecomment-844417211>, or unsubscribe < https://github.com/notifications/unsubscribe-auth/AAAC5342JT4XMIFR66JKMKDTOQIUFANCNFSM4NNJP7ZQ

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/twilio/twilio-node/issues/577#issuecomment-844418746, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALX4NJQBSY2LPWP4OPF4QWTTOQI5BANCNFSM4NNJP7ZQ .

mikeymckay commented 3 years ago

I assume you have read this in detail:

https://www.twilio.com/docs/api/errors/21211

(sending/receiving to the same number perhaps?)

Does it work for other numbers? I did find that there are some Ugandan phone numbers that Twilio won't send to, apparently because they are on a spam list, which seemed really odd since we were messaging teachers.

On Wed, May 19, 2021 at 10:01 PM Tumusiime Barnabus < @.***> wrote:

Any suggestions?

On Wed, May 19, 2021 at 10:53 PM Tumusiime Barnabus < @.***> wrote:

21211

On Wed, May 19, 2021 at 10:49 PM Mike McKay @.***> wrote:

What's the error code from Twilio?

On Wed, May 19, 2021 at 9:47 PM Tumusiime Barnabus @.***> wrote:

The challenge am having is that the api recognizes my phone number as invalid even when I use E.164 format On Wed, May 19, 2021 at 10:43 PM Mike McKay @.***> wrote:

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub < https://github.com/twilio/twilio-node/issues/577#issuecomment-844414742 , or unsubscribe <

https://github.com/notifications/unsubscribe-auth/ALX4NJRBPOQ45LN7JKUPQHLTOQIHRANCNFSM4NNJP7ZQ

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub < https://github.com/twilio/twilio-node/issues/577#issuecomment-844417211 , or unsubscribe <

https://github.com/notifications/unsubscribe-auth/AAAC5342JT4XMIFR66JKMKDTOQIUFANCNFSM4NNJP7ZQ

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub < https://github.com/twilio/twilio-node/issues/577#issuecomment-844418746>, or unsubscribe < https://github.com/notifications/unsubscribe-auth/ALX4NJQBSY2LPWP4OPF4QWTTOQI5BANCNFSM4NNJP7ZQ

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/twilio/twilio-node/issues/577#issuecomment-844427418, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAC5325GZ7NO46PKIMG3SLTOQKJDANCNFSM4NNJP7ZQ .

Rapiiidooo commented 1 year ago

@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 commented 1 year ago

tested with angular 14 SSR, you should lazy load it to prevent it leak to override global namespace URL of nodejs (mention here https://github.com/muaz-khan/RecordRTC/issues/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;
        });