Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: exphbs is not a function

I am trying to use the express-handlebars module but have an error. My code:

var exphbs = require('express-handlebars');

app.engine('.hbs', exphbs({ extname: '.hbs', defaultLayout: "main"}));
app.set('view engine', '.hbs');

My error:

"app.engine('.hbs', exphbs({ extname: '.hbs', defaultLayout: "main"}));
                   ^

TypeError: exphbs is not a function"

I can't seem to figure it out why is it not working properly. Any ideas?

like image 275
Adidi24 Avatar asked Sep 03 '25 14:09

Adidi24


2 Answers

You should use "exphbs.engine" instead of "exphbs".

app.engine('.hbs', exphbs.engine({ extname: '.hbs', defaultLayout: "main"}));
like image 147
lxu Avatar answered Sep 05 '25 02:09

lxu


You have two options to solve this

1st Method-

In this you just have to update a single word .enigne

Change this:

 app.engine('.hbs', exphbs({ extname: '.hbs', defaultLayout: "main"}));

To

app.engine('.hbs', exphbs.engine({ extname: '.hbs', defaultLayout: "main"}));

2nd Method-

In this, you have to update

var exphbs = require('express-handlebars'); 

To

 const { engine } = require('express-handlebars');

And

app.engine('.hbs', exphbs({ extname: '.hbs', defaultLayout: "main"}));

To

app.engine('handlebars', engine({ extname: '.hbs', defaultLayout: "main"}));

Goodluck

like image 25
Arslan Ahmad khan Avatar answered Sep 05 '25 02:09

Arslan Ahmad khan