skip to Main Content

Error is "app.engine("handlebars", exphbs()); ‘TypeError: exphbs is not a function
at Object. ‘"

const express = require('express')
const exphbs = require("express-handlebars");
const path = require("path");
const app = express()
const port = 3000

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

2

Answers


  1. I think its possible that the exphbs function is not properly installed or imported:

      npm install express-handlebars
    

    Import it with:

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

    If that’s not the case, and you have it installed and imported correctly. Then see if updating to the latest version fixes the issue:

      npm update express-handlebars
    

    EDIT: Now that I think about it, you need to call the engine() function on exphbs to get the actual function that you can use to register the handlebars templating engine with your Express app.

    The correct code should be:

      const express = require('express')
      const exphbs = require("express-handlebars");
      const path = require("path");
      const app = express()
      const port = 3000
    
      app.engine("handlebars", exphbs.engine());
      app.set("view engine", "handlebars");
    
    Login or Signup to reply.
  2. exphbs isn’t a function, it is an object of stuff exported by handlebars (see the documentation). The function you want to use is exphbs.engine() like this:

    app.engine("handlebars", exphbs.engine());
    

    Alternatively, you can destructure the object and take out engine directly:

    const express = require('express')
    const { engine } = require("express-handlebars");
    const path = require("path");
    const app = express()
    const port = 3000
    
    app.engine("handlebars", engine());
    app.set("view engine", "handlebars");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search