Search code examples
node.jsexpresshandlebars.jsexpress-handlebars

exphbs is not a function


I have been trying to test out express and express handlebars. I have read and tried a few demos. They all basically the same but I cannot get any of them to work. The error I keep getting is -

app.engine('handlebars', exphbs()); ^

TypeError: exphbs is not a function

here is my code below:

const express = require("express");
const exphbs = require("express-handlebars");
const app = express();
const port = 8000;

//Handelbars Middleware
app.engine("handlebars", exphbs());
app.set("view engine", "handlebars");

// Index Route
app.get("/", function (req, res) {
  res.render("home");
});

app.listen(port, () => {
  console.log(`Server started on port ${port}`);
});

I made a short video to further explain - https://www.awesomescreenshot.com/video/6352907?key=df18cdbdf4ed12b85d2c92458ad9a2de

I thought const exphbs = require('express-handlebars'); was declared a function

Thanks


Solution

  • exphbs is not function u can try this code..

    const express = require('express');
    const exphbs  = require('express-handlebars');
     // const { engine }  = require('express-handlebars');
     const app = express();
     const port = 8000;
    
    //Handelbars Middleware
    //app.engine('handlebars', engine());
    app.engine('handlebars', exphbs.engine());
    app.set('view engine', 'handlebars');
    
    // Index Route
    app.get('/', function (req, res) {
        res.render('home');
    });
    
    
    
    app.listen(port, () =>{
      console.log(`Server started on port ${port}`);
    });