nodemailer-express-handlebars icon indicating copy to clipboard operation
nodemailer-express-handlebars copied to clipboard

Path must be a string. Received undefined

Open johnlimed opened this issue 5 years ago • 2 comments

Stacktrace: at assertPath (path.js:28:11) at Object.resolve (path.js:1171:7) ExpressHandlebars._resolveLayoutPath

This issue is caused by the dependency: ExpressHandlebars.

Underlying issue is because of a change in ExpressHandlebars, it will use defaultLayout='main' no matter what, as such it expects the layoutDir to be a string however for users who do not use a layout, like in my case, it will throw this error 'Path must be a string ...'

Just putting this issue here so that people who might have this issue can know a quick fix:

Whenever you generate your mailOptions, set your context.layout=false. This would override the defaultLayout in ExpressHandlebars to be false and not produce this bug.

The real fix would have to be implemented by ExpressHandlebars which there are requests to remove the defaultLayout.

if a patch wants to be made in nodemailer-express-handlebars, I was thinking a quick fix could be adding the following to the render function:

TemplateGenerator.prototype.render = function render(mail, cb) {
    if (mail.data.html) return cb();

    var templatePath = path.join(this.viewPath, mail.data.template + this.extName);
    const { layout } = mail.data.context;
    if (!layout) mail.data.context.layout = false;
    this.viewEngine.renderView(templatePath, mail.data.context, function(err, body) {
        if (err) return cb(err);

        mail.data.html = body;
        cb();
    });
};

johnlimed avatar Jun 06 '19 14:06 johnlimed

I have the same issue here

hzburki avatar Jun 13 '19 09:06 hzburki

@johnlimed @hzburki I ran into a similar issue and fixed it by adding defaultLayout: false to handlebars options:

  const options = {
    viewEngine: {
      extName: ".hbs",
      partialsDir: path.resolve(__dirname, "templates"),
      defaultLayout: false        // <-----   added this
    },
    viewPath: path.resolve(__dirname, "templates"),
    extName: ".hbs"
  };

  this.transporter.use("compile", hbs(options));

nbrookie avatar Jun 17 '19 03:06 nbrookie