12

i have made the handlebars template config on my index.js file, but when i try to run my code with nodemon he returns me this error. anyone can help?

enter image description here

enter image description here

Marilia Soares
  • 121
  • 1
  • 1
  • 4

3 Answers3

28
Problem

Your initialization of engine on line 8 is wrong.

Solution

Please change it to app.engine('handlebars', engine());. Check the snippet below.

Implementation
const express = require('express');
const { engine } = require ('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(3000);
Reference
Salvino D'sa
  • 4,018
  • 1
  • 7
  • 19
8

Insert .engine after handlebars, look:

app.engine('handlebars', handlebars.engine({ defaultLayout: 'main' }));
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
1

const { engine } = require("express-handlebars");

app.engine( "hbs", engine({ extname: "hbs", defaultLayout: false, layoutsDir: "views/layouts/", }) );

  • Welcome to SO. Please write the necessary details in your answer. Code-only answers are not helpful for readers. Please read this https://stackoverflow.com/help/how-to-answer – Muhammad Tariq Apr 02 '22 at 02:16
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 02 '22 at 02:16