How can I register and use a Handlebars helper with Node?

handlebars.js, javascript, node.js

Solution

I figured it out. I needed to do this:

var Handlebars = require('handlebars/runtime')['default'];

One what's really cool is, this even works in the browser with Browserify.

However, I found that an even better way (and probably the "correct" way) is to precompile Handlebars templates via (shell command):

handlebars ./templates/ -c handlebars -f templates.js

Then I do this:

var Handlebars = require('handlebars');
require('./templates');
require('./helpers/logic');

module.exports.something = function() {
    ...
    template = Handlebars.templates['template_name_here'];
    ...
};

Problem

I'm using Handlebars with Node, and that works fine: ``` require('handlebars'); var template = require('./templates/test-template.handlebars'); var markup = template({ 'some': 'data' }); console.log(markup); ``` That works fine. However, I need to register and use a custom helper in my template. So, now my code looks like this: ``` var Handlebars = require('handlebars'); Handlebars.registerHelper('ifEqual', function(attribute, value) { if (attribute == value) { return options.fn(this); } else { return options.inverse(this); } }); var template = require('./templates/test-template.handlebars'); var markup = template({ 'some': 'data' }); console.log(markup); ``` But now when I run my script, I get Error: Missing helper: 'ifEqual' So: how can I define and use a custom helper in Node?

Original source