Nodejs error: A partials dir must be a string or config Object

Asked

Viewed 1,663 times

0

I am sending standardized emails using nodemailer with nodemailer-express-handlebars ,but whenever I am trying to send an email, I am receiving the

Error: A partials dir must be a string or config Object.

I don’t know what the problem is.

const path =  require('path');
const nodemailer = require ('nodemailer');
const hbs = require('nodemailer-express-handlebars');

const { host, port, user, pass } = require('../config/mail.json');

const transport = nodemailer.createTransport({
    host,
    port,
    auth: {user, pass },
  });

  transport.use('compile', hbs({
      viewEngine: 'handlebars',
      viewPath: path.resolve('./resources/mail/'),
      extName: '.html',
  }));

  module.exports = transport; 

3 answers

4

I had that same mistake, and the solution I found was this: in your project, open node_modules/express-handlebars/lib/express-handlebars.js. InlayoutsDirputs the template directory and repeats the same thing to the partialsDir. In defaultLayout leaves as undefined.

function ExpressHandlebars(config) {
  utils.assign(
    this,
    {
      handlebars: Handlebars,
      extname: ".handlebars",
      layoutsDir: "./src/resources/mail/", 
      partialsDir: "./src/resources/mail/", 
      defaultLayout: undefined,
      helpers: undefined,
      compilerOptions: undefined
    },
    config
  );

I hope it helps you!

1

Just inform the defaultLayout as Undefined and the partialsDir with your path. Follow the code snippet to be corrected:

transport.use('compile', hbs({
    viewEngine: {
        defaultLayout: undefined,
        partialsDir: path.resolve('./src/resources/mail/')
      },
      viewPath: path.resolve('./src/resources/mail/'),
      extName: '.html'
}));

1


Searching on the subject I found this link that can be useful: https://github.com/yads/nodemailer-express-handlebars/issues/22

Looks like it needs you to add the property partialsDir in the Handlebar options.

Follow below example taken from the link I sent above:

const handlebarOptions = {
  viewEngine: {
    extName: '.hbs',
    partialsDir: 'some/path',
    layoutsDir: 'some/path',
    defaultLayout: 'email.body.hbs',
  },
  viewPath: 'some/path',
  extName: '.hbs',
};

transporter.use('compile', hbs(handlebarOptions));

I hope it helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.