Home > Back-end >  exphbs is not a function
exphbs is not a function

Time:12-09

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

CodePudding user response:

I think You should use example as reference from express-handlebars. To use this code you need add to your pacakge.json file this line "type": "module",(ES6 import) Good Luck!

import express from 'express';
import { engine } from '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(8000);

CodePudding user response:

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}`);
});
  • Related