Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MailJet require().connect is not a function

I'm attempting to verify sending with MailJet, but I literally can't, since it's throwing a stupid error.

This is what error I'm encountering.

const mailjet = require('node-mailjet').connect('****************************1234', '****************************abcd')
                                        ^

TypeError: require(...).connect is not a function
    at Object.<anonymous> (C:\Users\code\Downloads\asd\mail.js:1:41)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

I'm using the exact same code it has here, https://app.mailjet.com/auth/get_started/developer

const mailjet = require ('node-mailjet')
.connect('****************************1234', '****************************abcd')
const request = mailjet
.post("send", {'version': 'v3.1'})
.request({
  "Messages":[
    {
      "From": {
        "Email": "[email protected]",
        "Name": "example"
      },
      "To": [
        {
          "Email": "[email protected]",
          "Name": "example"
        }
      ],
      "Subject": "Greetings from Mailjet.",
      "TextPart": "My first Mailjet email",
      "HTMLPart": "<h3>Dear passenger 1, welcome to <a href='https://www.mailjet.com/'>Mailjet</a>!</h3><br />May the delivery force be with you!",
      "CustomID": "AppGettingStartedTest"
    }
  ]
})
request
  .then((result) => {
    console.log(result.body)
  })
  .catch((err) => {
    console.log(err.statusCode)
  })
like image 704
Bee Avatar asked Jan 24 '26 23:01

Bee


1 Answers

You can change version in package.json to use ^3.3.2 you are using an old version.

version ^3.3.2

const mailjet = require ('node-mailjet')
 .connect(process.env.MJ_APIKEY_PUBLIC || '****************************1234', process.env.MJ_APIKEY_PRIVATE || '****************************abcd')
const request = mailjet
 .post("send", {'version': 'v3.1'})
 .request({
    "Messages":[{
        "From": {
            "Email": "[email protected]",
            "Name": "Mailjet Pilot"
        },
        "To": [{
            "Email": "[email protected]",
            "Name": "passenger 1"
        }],
        "Subject": "Your email flight plan!",
        "TextPart": "Dear passenger 1, welcome to Mailjet! May the delivery force be with you!",
        "HTMLPart": "<h3>Dear passenger 1, welcome to <a href=\"https://www.mailjet.com/\">Mailjet</a>!</h3><br />May the delivery force be with you!"
    }]
})
request
 .then((result) => {
    console.log(result.body)
 })
 .catch((err) => {
    console.log(err.statusCode)
 })

version 5.1.1

const Mailjet = require('node-mailjet');
const mailjet = Mailjet.apiConnect(
  process.env.MJ_APIKEY_PUBLIC || '****************************1234',
  process.env.MJ_APIKEY_PRIVATE || '****************************abcd',
);

const request = mailjet
    .post('send', { version: 'v3.1' })
    .request({
      Messages: [
        {
          From: {
            Email: "[email protected]",
            Name: "Mailjet Pilot"
          },
          To: [
            {
              Email: "[email protected]",
              Name: "passenger 1"
            }
          ],
          Subject: "Your email flight plan!",
          TextPart: "Dear passenger 1, welcome to Mailjet! May the delivery force be with you!",
          HTMLPart: "<h3>Dear passenger 1, welcome to <a href=\"https://www.mailjet.com/\">Mailjet</a>!</h3><br />May the delivery force be with you!"
        }
      ]
    })

request
 .then((result) => {
    console.log(result.body)
 })
 .catch((err) => {
    console.log(err.statusCode)
 })
like image 184
MERLIN S Avatar answered Jan 26 '26 15:01

MERLIN S