Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: handlebars is not a function

i have made the handlebars template config on my index.js file, but when i try to run my code with nodemon he returns me this error. anyone can help?

enter image description here

enter image description here

like image 626
Marilia Soares Avatar asked Mar 03 '26 14:03

Marilia Soares


2 Answers

Problem

Your initialization of engine on line 8 is wrong.

Solution

Please change it to app.engine('handlebars', engine());. Check the snippet below.

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

const app = express();

app.engine('handlebars', engine());
app.set('view engine', 'handlebars');
app.set("views", "./views");

app.get('/', (req, res) => {
    res.render('home');
});

app.listen(3000);
Reference
  • Express-Handlebars
like image 53
Salvino D'sa Avatar answered Mar 05 '26 04:03

Salvino D'sa


Insert .engine after handlebars, look:

app.engine('handlebars', handlebars.engine({ defaultLayout: 'main' }));
like image 38
Gabriel Carvalho Avatar answered Mar 05 '26 04:03

Gabriel Carvalho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!