Registerhelper in node js express hbs

I want to add two numbers in node js express handlebars. So i found i can register helper like this

hbs.registerHelper('addTwoNum', function(num1, num2) {
  return num1+num2;
});

But i don’t know that where is the ‘hbs’ to register this helper. Note that i am using node js express

When i try this mehod

app.engine('hbs', handlebars({
  defaultLayout: 'mainlayout',
  helpers: {
      getTotal: function (num1, num2){
          
          return num1+num2;
      }   
  }
}));

It shows the error

app.engine('hbs', handlebars({
              ^

TypeError: handlebars is not a function
    at Object.<anonymous> (/home/adhil/mamans-store/app.js:27:19)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/home/adhil/mamans-store/bin/www:7:11)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

Answer

var express = require('express');
var exphbs  = require('express-handlebars');
 
var app = express();
 
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');

If you get this error

Error: Failed to lookup view "error" in views directory "/home/adhil/mamans-store/views"
    at Function.render (/home/adhil/mamans-store/node_modules/express/lib/application.js:580:17)
    at ServerResponse.render (/home/adhil/mamans-store/node_modules/express/lib/response.js:1008:7)
    at /home/adhil/mamans-store/app.js:80:7
    at Layer.handle_error (/home/adhil/mamans-store/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/home/adhil/mamans-store/node_modules/express/lib/router/index.js:315:13)
    at /home/adhil/mamans-store/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/adhil/mamans-store/node_modules/express/lib/router/index.js:335:12)
    at next (/home/adhil/mamans-store/node_modules/express/lib/router/index.js:275:10)
    at Layer.handle_error (/home/adhil/mamans-store/node_modules/express/lib/router/layer.js:67:12)
    at trim_prefix (/home/adhil/mamans-store/node_modules/express/lib/router/index.js:315:13)
    at /home/adhil/mamans-store/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/adhil/mamans-store/node_modules/express/lib/router/index.js:335:12)
    at next (/home/adhil/mamans-store/node_modules/express/lib/router/index.js:275:10)
    at /home/adhil/mamans-store/node_modules/express/lib/router/index.js:635:15
    at IncomingMessage.next (/home/adhil/mamans-store/node_modules/express/lib/router/index.js:260:14)
    at done (/home/adhil/mamans-store/node_modules/express/lib/response.js:1003:25)
    at Function.render (/home/adhil/mamans-store/node_modules/express/lib/application.js:582:14)
    at ServerResponse.render (/home/adhil/mamans-store/node_modules/express/lib/response.js:1008:7)
    at PRODUCT_HELPER.getCategorisedProducts (/home/adhil/mamans-store/routes/user.js:17:17)
    at Object.getCategorisedProducts (/home/adhil/mamans-store/helpers/PRODUCT_HELPER.js:247:9)
    at PRODUCT_HELPER.getAllCarouselItems (/home/adhil/mamans-store/routes/user.js:16:24)
    at Object.getAllCarouselItems (/home/adhil/mamans-store/helpers/PRODUCT_HELPER.js:219:9)

Rename your ‘error.hbs’ file into ‘error.handlebars’

That means you have to rename your all ‘example.hbs’ files into ‘example.handlebars’

Leave a Reply

Your email address will not be published. Required fields are marked *