Radu
Radu

Reputation: 11

TypeError: Vonage is not a constructor

I'm trying to send an SMS message to a random phone number from a node js file. I'm using my Vonage account. When I try this from the web page of the Vonage (https://dashboard.nexmo.com/getting-started/sms), it works perfectly. But when I run the code they suggest, it doesn't work anymore and I get this error: TypeError: Vonage is not a constructor+ After I installed the package (npm install @vonage/server-sdk), I created a js file with this code:

const Vonage = require('@vonage/server-sdk')

const vonage = new Vonage({
  apiKey: "27945288",
  apiSecret: "************Ek95"
})

const from = "Vonage APIs"
const to = "*********13"
const text = 'A text message sent using the Vonage SMS API'

async function sendSMS() {
    await vonage.sms.send({to, from, text})
        .then(resp => { console.log('Message sent successfully'); console.log(resp); })
        .catch(err => { console.log('There was an error sending the messages.'); console.error(err); });
}

sendSMS();

I run it with: "node sendSMS.js"

Upvotes: 0

Views: 542

Answers (4)

JOSPHAT NDHLOVU
JOSPHAT NDHLOVU

Reputation: 1

Make the following changes on your imports and package file

Convert

const Vonage = require('@vonage/server-sdk')

To

import { Vonage } from "@vonage/server-sdk"

Them add

"type":"module" 

in package.json

Upvotes: 0

Steven Yambos
Steven Yambos

Reputation: 3

You need to import the library with the correct ES6 syntax.

From :

const Vonage = require('@vonage/server-sdk')

To:

import { Vonage } from "@vonage/server-sdk"

Upvotes: 0

jonas
jonas

Reputation: 11

It has to be

const { Vonage } = require('@vonage/server-sdk')

not

const Vonage = require('@vonage/server-sdk')

Upvotes: 1

P1otr
P1otr

Reputation: 1

In your package.json make sure you are using Vonage version 3 not 2. Version 2 doesn't support this.

Upvotes: 0

Related Questions