Node.js中使用Vonage SDK发送SMS报错:TypeError: Vonage is not a constructor
解决Vonage SDK "TypeError: Vonage is not a constructor" 问题
你遇到的问题是因为@vonage/server-sdk的新版本(v3及以上)不再采用默认导出Vonage类,而是使用命名导出,原来的导入写法已经不适用了。
修正步骤:
- 调整导入语句,改用解构方式获取
Vonage类:
const { Vonage } = require('@vonage/server-sdk')
- 新版本中
sms.send的参数结构有更新,同时建议在async函数里统一用try/catch替代then/catch(避免混用await和链式调用),修正后的完整代码如下:
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() { try { const resp = await vonage.sms.send({ to, from, text }); console.log('Message sent successfully'); console.log(resp); } catch (err) { console.log('There was an error sending the messages.'); console.error(err); } } sendSMS();
- 仍然用
node sendSMS.js命令执行即可。
内容的提问来源于stack exchange,提问作者Radu
相关产品推荐
相关产品推荐

