Asher
Asher

Reputation: 403

NodeJs with Express and Handlebars - handlebars.engine is undefined

I am following the tutorials in O'Reilly's "Web Development with Node & Express" by Ethan Brown.

They use handlebars as the view engine.

Here is my code:

var express = require ('express'),
    handlebars = require('express3-handlebars'),
    app = express();

handlebars.create({ defaultLayout: 'main' });
app.engine('handlebars', handlebars.engine);

The problem I am having is that handlebars.engine is undefined, resulting in a "Callback function expected" error when running the application.

I have tried searching online without any luck.

Is this some legacy syntax with handlebars? My packages have installed fine and I have tried reinstalling them.

Is there a fix/updated code for this?

Upvotes: 1

Views: 2851

Answers (1)

NeedCoffee
NeedCoffee

Reputation: 70

You have to get the engine from the object you got from the create()-call!

Like this: var expHbs = require('express-handlebars'); var handlebars = expHbs.create({ defaultLayout: 'layout', extname: '.hbs', helpers: handlebarsHelpers }); app.engine('.hbs', handlebars.engine); app.set('view engine', '.hbs');

Just saying: express3-handlebars got renamed to express-handlebars. You should consider switching.

Upvotes: 2

Related Questions