How use "new" operator with resend?

319 views Asked by At

My application in nodejs need send transactional email using the resend library. I installed the resend package with npm i resend and i did the code below.

const resend = require("resend");
const instanceResend = new resend(process.env.KEY_RESEND);

const sendMail = async (from, to, subject, html) => {
  try {
    const data = await instanceResend.emails.send({
      from: from,
      to: to,
      subject: subject,
      html: html,
      text: text,
      headers: {
        "X-Entity-Ref-ID": process.env.KEY_RESEND,
      },
      tags: [
        {
          name: "category",
          value: "reset_password",
        },
      ],
    });
    console.log("Email data: ", data);
    return data;
  } catch (error) {
    console.error(error);
    return error;
  }
};

module.exports = sendMail;

In controllers I called the file sendEmail through of require and use in the function forgotPassword ( used to send password recovery email). Look the code below.

const sendMail = require("../utils/sendMail");

 forgotPassword: async (req, res) => {
    const { email } = req.body;
    try {
      const user = await userModel.findOne({ email });
      if (!email || !user)
        return res
          .status(400)
          .json({ message: "Email not found or invalid", success: false });

      await sendMail(
        "[email protected]",
        "[email protected]",
        "[email protected]",
        "<p>Hello</p",
        "TEXT",
        (err) => {
          if (err)
            return res.status(400).json({
              message: `Can not send forgot password email. ${err}`,
              success: false,
            });

          return res.status(200).json({
            message: "Forgot password email sent successfully: ",
            success: true,
          });
        }
      );
    } catch (error) {
      res.status(400).json({
        message: "Erro on forgot password, try again",
        error,
        success: false,
      });
      return error;
    }
  },

I expected the email to be sent, however, I'm getting the error below in the vscode terminal:

const instanceResend = new resend(process.env.KEY_RESEND);
                        ^
TypeError: resend is not a constructor

The new operator is causing an error in my code. How to solve it? Am I using node with javascript?

Site resend: here

1

There are 1 answers

0
Brettski On BEST ANSWER

Based on the examples I see at https://www.npmjs.com/package/resend you are not requiring resend correctly. You are getting the whole resend library, where you need to only get the Resend property:

Your code:

const resend = require("resend");
const instanceResend = new resend(process.env.KEY_RESEND);

How your code should be based on the examples provided:

const { Resend } = require("resend");
const instanceResend = new Resend(process.env.KEY_RESEND);

It is the same as, which is just a longer form:

const resend = require("resend");
const { Resend } = resend;