'TypeError: expressHandlebars is not a function

When using express-handlebars module,there encounter an error that saying,

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

TypeError: expressHandlebars is not a function

My code is as follows

const express = require('express')
const expressHandlebars = require('express-handlebars')
const app = express()
// configure Handlebars view engine
app.engine('handlebars', expressHandlebars({
     defaultLayout: 'main',
}))
app.set('view engine', 'handlebars')
..........

I can't find any mistake in here because above code snippet is purely from book that I refer. But it doesn't seem to work.

I will be grateful if someone can to tell that is this because version different of express-handlebars between me and the book I refer or, Is there any mistake in the code.



Solution 1:[1]

You should require the express handlebars engine instead of the whole expressHandlebars package.

Change:

const expressHandlebars = require('express-handlebars')

To:

const expressHandlebars = require('express-handlebars').engine;

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 Josh Cronin