ReferenceError: Handlebars is not defined

Why I am getting this error when I start express handlebar application ?

ReferenceError: Handlebars is not defined
    at Object.<anonymous> (D:\....\public\js\helpers.js:1:63)

my code

helpers.js

Handlebars.registerHelper('ifEquals', function(arg1, arg2, options) {
    return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
});

app.js

// view engine setup
app.engine('hbs', hbs({helpers: require("./public/js/helpers.js").helpers,extname: 'hbs', defaultLayout: 'layout', layoutsDir: __dirname + '/views/layouts/'}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

what is going wrong ? how to fix this ?

It looks like the Handlebars library hasn’t been included.

I’m not familiar with Express, but there looks to be a couple of good leads in the following articles:

How to Use Jade and Handlebars in Express.js

How to use Handlebars with Express

It looks like the Handlebars library hasn’t been included.

I already have handlebars library. please see code below

app.js

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

// view engine setup
app.engine('hbs', hbs({helpers: require("./public/js/helpers.js").helpers,extname: 'hbs', defaultLayout: 'layout', layoutsDir: __dirname + '/views/layouts/'}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

helpers.js

Handlebars.registerHelper('ifEquals', function(arg1, arg2, options) {
    return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
});

hbs page

 {{# each data}}
{{#ifEquals indexName "NIFTY FMCG") }}
	<tr>
	<td>{{indexName}}</td> 
	<td>{{percChange}}</td> 
	</tr>
{{//ifEquals}}
{{/each}}

Still getting error
ReferenceError: Handlebars is not defined at Object.&lt;anonymous&gt; (D:\....\public\js\helpers.js:1:63)

Clearly your code cannot find the Handlebars library.

That could be for one of a few reasons. Either the file doesn’t exist, or your code isn’t properly referencing the handlebars library.

You’re going to need someone familiar with Express to help you with this issue.

AFAICT you haven’t required handlebars anywhere, just express-handlebars and only in your app.js and not in the helpers.js (when imported as modules, libraries don’t pollute the global namespace so you have to require them anywhere they’re being used). But actually you don’t register them with registerHelper() anyway, but directly define them in the config object – see here for details.

I have changed code to this. Now there is no reference error.

But I’m facing another error in while comparing String values here

{{# each data}}
{{#ifEquals indexName NIFTY FMCG) }}
	<tr>
	<td>{{indexName}}</td> 
	<td>{{percChange}}</td> 
	</tr>
{{//ifEquals}}
{{/each}}

Error
# Parse error on line 255: ...indexName NIFTY FMCG) }} &lt;tr&gt; -----------------------^ Expecting 'CLOSE', 'OPEN_SEXPR', 'ID', 'OPEN_BLOCK_PARAMS', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'CLOSE_SEXPR'

app.js

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

// view engine setup
app.engine('hbs', hbs({helpers: require("./public/js/helpers.js").helpers,extname: 'hbs', defaultLayout: 'layout', layoutsDir: __dirname + '/views/layouts/'}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

helpers.js

var register = function(Handlebars) {
  var helpers = {
    // put all of your helpers inside this object
    ifEquals: function(){
        return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
    }
  };

  if (Handlebars && typeof Handlebars.registerHelper === "function") {
    // register helpers
    for (var prop in helpers) {
        Handlebars.registerHelper(prop, helpers[prop]);
    }
  } else {
      // just return helpers object if we can't register helpers here
      return helpers;
  }

};

module.exports.register = register;
module.exports.helpers = register(null); 

It look like there’s an unmatched parenthesis there.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.