Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
Using Nodemailer Express Handlebars with Templates

I'm trying to use the template feature to send emails but every time I try it returns this same error:

[Error: ENOENT: no such file or directory, open 'C:\Projetos\project\backend.io\main.handlebars'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\\Projetos\\project\\backend.io\\main.handlebars'
}

I'm putting down my controller which is used in my route when I call the store method:

class Players extends Controller {
  constructor() {
    super(Player);
    this.JWT_EXPIRE = 28000;
    this.relationships = ["permission"];
    this.fieldHidden = ['fieldHidden']
  }

  async store(req, res) {
    try {
      let body = req.body;

      if (body.password) {
        const salt = await bcrypt.genSalt(10);
        body.password = await bcrypt.hash(body.password.toString(), salt);
      }

      req.body = body;

      let player = super.store(req, res);

      Mail.sendMail({
        from: 'Email <muyemail@myhostname.com>',
        to: player.email,
        subject: 'Reset your password',
        template: 'confirm_account', //Name email file template
        context: { // pass variables to template          
          token: player.token
        }
      }, function (err, response) {
        if (err) {
          console.error(err)
        }
        console.log(response)
      });

      return player;
    } catch (error) {
      let errors = error.message.split(",\n");
      return res.json({
        status: "error",
        message: errors,
      });
    }
  }
}

This one is my Configuration file that I made to manage the sending of the Email:

"use strict";
import nodemailer from 'nodemailer';
import dotenv from 'dotenv';
import mailerhbs from 'nodemailer-express-handlebars';
const path = require('path');
dotenv.config();

// create reusable transporter object using the default SMTP transport
var Mail = nodemailer.createTransport({
    host: process.env.MAIL_HOST,
    port: 465,
    secure: true, // true for 465, false for other ports
    auth: {
      user: process.env.MAIL_USERNAME,
      pass: process.env.MAIL_PASSWORD
    },
});

Mail.use('compile', mailerhbs({
    viewPath: 'src/mails/templates', //Path to email template folder
    extName: '.hbs' //extendtion of email template
}));

export default Mail;

My structure project image below:

enter image description here

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!