'Why is handlebars not working in Node.js?

I have written the line:

app.engine('hbs',hbs({extanme:'hbs',defaultLayout:'layout',layoutsDir:__dirname+'/views/layout/',partialsDir:__dirname+'/views/partials/'}));

And I have assigned:

var hbs= require('handlebars');

The error I get is:

hbs is not a function



Solution 1:[1]

Try something like this

npm i hbs // install hbs package

//app.js
const express = require('express')
const path = require('path')
const hbs = require('hbs')
const app = express()

// View Engine Setup
app.set('views', path.join(__dirname))
app.set('view engine', 'hbs')

app.get('/', function(req, res){
    res.render('Home', {
    array: ['One', 'Two', 'Three', 'Four'],
    message: 'Greetings from geekforgeeks'
    })
})

app.listen(8080, function(error){
    if(error) throw error
    console.log("Server created Successfully")
})

Solution 2:[2]

For your app.engine after referencing the NPM express-handlebar docs here: https://www.npmjs.com/package/express-handlebars. You need to add a .engine to your hbs constant.

try this!

var hbs= require('handlebars');

app.engine('hbs', hbs.engine('your config stuff here'));

If your still running into issues. Try using express-handlebars (npm install express express-handlebars) with express, then require them like you do. Then it should work

Solution 3:[3]

Try This!!

Add hbs.engine

app.engine('hbs',hbs.engine({extname:'hbs',defaultLayout:'layout',layoutsDir:__dirname+'/views/layout/',partialsDir:__dirname+'/views/partials/'}));

And assign: var hbs= require('express-handlebars');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jatin Mehrotra
Solution 2 Michael Barreiro
Solution 3 DASHSTRO gaming