4

How to import twilio into nodejs es module?

const client = require("twilio")(YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN);
JohannesAndersson
  • 4,550
  • 2
  • 17
  • 30
SavceBoyz
  • 59
  • 5

2 Answers2

4

In the library (you can see here):

module.exports = function(accountSid, authToken, opts) {
    return new Twilio(accountSid, authToken, opts);
}

module.exports = Car would be equivalent to export default Car

So you can import the module with:

import twilio from "twilio";
//     ^ this will select default export

const client = twilio(YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN);
syahid246
  • 109
  • 2
  • 7
0

You can do this:

import * as Twilio from 'twilio';

const YOUR_AUTH_TOKEN = process.env.TWILIO_AUTH_TOKEN;
const YOUR_ACCOUNT_SID = process.env.TWILIO_ACCOUNT_SID;


const client = Twilio(YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN);

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

Make sure you install twilio correctly.

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35