TypeError: Handlebars.registerHelper is not a function

Sho Nuff picture Sho Nuff · Nov 29, 2015 · Viewed 8.7k times · Source

I am brand new to node and handlebars as of two days ago so bear with me. I am trying to use custom handlebars helpers but am not entirely sure where to put it.

I keep getting "TypeError: Handlebars.registerHelper is not a function"

Right now I have it in my server.js file. Not sure if this is correct.

var express  = require('express');
var app      = express();
var Handlebars  = require('express-handlebars');


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

app.set('view engine', 'handlebars');

Handlebars.registerHelper("if", function(conditional, options) {
  if (options.hash.desired === options.hash.type) {
    options.fn(this);
  } else {
    options.inverse(this);
  }
});

Answer

R.A. Lucas picture R.A. Lucas · Nov 29, 2015

It looks like you need to use it like this, according to the docs found here: https://github.com/ericf/express-handlebars

var hbs = Handlebars.create({
  // Specify helpers which are only registered on this instance.
  helpers: {
    foo: function () { return 'FOO!'; },
    bar: function () { return 'BAR!'; }
  }
});