forwardemail / email-templates

Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for @forwardemail, @ladjs, @cabinjs, @spamscanner, and @breejs.
https://forwardemail.net/docs/send-emails-with-node-js-javascript
MIT License
3.67k stars 337 forks source link

Error: Engine not found for the ".hbs" file extension #276

Closed sean-hill closed 6 years ago

sean-hill commented 6 years ago

Hey there, I'm using handlebars as my templating engine. I have a html.hbs file in my project and have handlebars installed locally in my project. I have my options setup like this:

const email = new Email({
  views: {
    root: path.resolve(__dirname, `templates`),
    options: {
      extension: 'hbs'
    }
  }
})

But I'm receiving a:

Error: Engine not found for the ".hbs" file extension

error when trying to render the template. Any suggestions?

niftylettuce commented 6 years ago

I think it might have to do with handlebars vs hbs extension. I remember a similar issue with nunjucks engine in the past. Unsure though, have you tried renaming your file extensions to .handlebars?

https://github.com/tj/consolidate.js/blob/master/lib/consolidate.js#L171-L189

niftylettuce commented 6 years ago

I believe I see exactly what issue is, we have to do a line similar to https://github.com/tj/consolidate.js/blob/master/lib/consolidate.js#L1610.

exports.hbs = fromStringRenderer('handlebars');
niftylettuce commented 6 years ago

Want to test this out locally and submit a pull request to the consolidate repo?

Basically you'd do this:

  1. Fork email-templates GitHub repo, clone it locally, go inside, run yarn and then npm link.
  2. Fork consolidate GitHub repo, clone it locally, go inside, run npm install, and then npm link.
  3. Go into email-templates folder, run npm link consolidate
  4. Go into consolidate, add the code into the lib/consolidate.js file
  5. Go into your project, run npm link email-templates, and then run it again
  6. Test it out and if it works submit pull request to consolidate repo

cc @sean-hill @polonel

md-fileshadow commented 6 years ago

Just to let you know I also have this issue. I've forked the repos and done the edit to consolidate, but still it does not work for me. I'm getting a "Cannot find module 'handlebars'" error, but I might not have managed my dependencies correctly. I will keep working on it but wanted you to know this really breaks my email system since all of my templates are using handlebars.

niftylettuce commented 6 years ago

npm install handlebars in the root of your project?

md-fileshadow commented 6 years ago

Yes, handlebars has been installed in the root of the project but still not working. But I found a workaround here:

https://github.com/niftylettuce/email-templates/issues/149

Renaming my template files with .handlebars extensions and adding the following to my code before doing a renderAll() works:

// Override consolidate's handlebars instance email.config.views.options.engineSource.requires.handlebars = Handlebars;

Sorry I don't have more time to pursue this to get the .hbs extensions working, but thanks for the response!

BrianDV commented 6 years ago

I had the same error. I added mapping and had no further issues:

const email = new Email({
  views: {
    root: path.resolve(__dirname, `templates`),
    options: {
      extension: 'hbs',
      map: {
        'hbs': 'handlebars'
      }
    }
  }
})
niftylettuce commented 6 years ago

Should be resolved now, try v3.4.0. I've added the default mapping to of hbs to handlebars per the PR in #284.

petersaints commented 6 years ago

I'm not sure if I should open a new issue just to say that you must also map 'njk' to nunjucks:

views: {
    options: {
      extension: 'njk',
      map: {
        'njk': 'nunjucks'
      }
    }
  }

It would be nice if you could also add it as a default.

niftylettuce commented 6 years ago

@petersaints I've added this in v3.5.1 of email-templates. The only thing you will need to do is specify the views.options.extension: 'njk' option. The mapping of njk: 'nunjucks' has been added as a default similar to hbs => handlebars above. npm install email-templates@latest or yarn add email-templates@latest. thanks for request.