You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用MailJet发送邮件时遇TypeError:require(...).connect不是函数求助

解决node-mailjet的TypeError: require(...).connect is not a function错误

这个错误是因为你安装的node-mailjet版本为v5及以上,而官方入门文档的示例代码是针对v4及以下旧版本编写的——新版本API已移除.connect()方法,调用方式发生了变更。

解决方案1:适配v5+版本的正确写法

替换初始化代码,直接创建Mailjet实例:

const { Mailjet } = require('node-mailjet');
const mailjet = new Mailjet({
  apiKey: '****************************1234',
  apiSecret: '****************************abcd'
});

// 后续发送逻辑基本沿用原示例,版本参数位置不变
const request = mailjet
  .post('send', { version: 'v3.1' })
  .request({
    "Messages": [{
      "From": {
        "Email": "socials@example.xyz",
        "Name": "example"
      },
      "To": [{
        "Email": "socials@example.xyz",
        "Name": "example"
      }],
      "Subject": "Greetings from Mailjet.",
      "TextPart": "My first Mailjet email",
      "HTMLPart": "<h3>Dear passenger 1, welcome to Mailjet!</h3><br />May the delivery force be with you!",
      "CustomID": "AppGettingStartedTest"
    }]
  });

request
  .then((result) => {
    console.log(result.body)
  })
  .catch((err) => {
    console.log(err.statusCode)
  })

解决方案2:降级到v4.x版本(匹配官方示例)

如果想完全沿用官方入门代码,可卸载当前版本并安装v4.x最新版:

npm uninstall node-mailjet
npm install node-mailjet@4

安装完成后,官方提供的示例代码即可正常运行。

内容的提问来源于stack exchange,提问作者Bee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 11:20:35