Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

2.3K
Visualizações
TypeError: exphbs is not a function

I am trying to use the express-handlebars module but have an error. My code:

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

app.engine('.hbs', exphbs({ extname: '.hbs', defaultLayout: "main"}));
app.set('view engine', '.hbs');

My error:

"app.engine('.hbs', exphbs({ extname: '.hbs', defaultLayout: "main"}));
                   ^

TypeError: exphbs is not a function"

I can't seem to figure it out why is it not working properly. Any ideas?

over 4 years ago · Santiago Trujillo
8 Respostas
Responde à pergunta

0

You have two options to solve this

1st Method-

In this you just have to update a single word .enigne

Change this:

 app.engine('.hbs', exphbs({ extname: '.hbs', defaultLayout: "main"}));

To

app.engine('.hbs', exphbs.engine({ extname: '.hbs', defaultLayout: "main"}));

2nd Method-

In this, you have to update

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

To

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

And

app.engine('.hbs', exphbs({ extname: '.hbs', defaultLayout: "main"}));

To

app.engine('handlebars', engine({ extname: '.hbs', defaultLayout: "main"}));

Goodluck

over 4 years ago · Santiago Trujillo Relatório

0

that's right, per documentation from hbs, this option will get the job done.

import express from 'express';
import { engine } from 'express-handlebars';

const app = express();

app.engine('.hbs', engine({extname: '.hbs'}));
app.set('view engine', '.hbs');
app.set('views', './views');
over 4 years ago · Santiago Trujillo Relatório

0

If you want to use express-handlebars with commonJS module you should import it like that:


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

const app = express();

app.engine('handlebars', engine({ extname: '.hbs', defaultLayout: "main"}));
app.set('view engine', 'handlebars');
app.set("views", "./views");

app.get('/', (req, res) => {
    res.render('home');
});

app.listen(3000);

If you want to go with ES6 module, you can follow the official readme

over 4 years ago · Santiago Trujillo Relatório

0

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

and

app.engine('handlebars', engine({ extname: '.hbs', defaultLayout: "main"}));
over 4 years ago · Santiago Trujillo Relatório

0

You can use exphbs liabrary instead of express-handlebars.

Registering view engine:

// view engine setup
app.engine('hbs', require('exphbs'));
app.set('view engine', 'hbs');
app.set('view options', { layout: 'main'});

By default,exphbs is looking for a folder name called partials for partials directory and folder name called layouts for layouts directory.Both partials and layouts folders come under views directory.

learn more

over 4 years ago · Santiago Trujillo Relatório

0

You should use "exphbs.engine" instead of "exphbs".

app.engine('.hbs', exphbs.engine({ extname: '.hbs', defaultLayout: "main"}));
over 4 years ago · Santiago Trujillo Relatório

0

  1. var exphbs = require("express-handlebars");
  2. app.engine("handlebars",exphbs({ defaultLayout: "main" }));

Replace with this :

  1. const { engine } = require("express-handlebars");
  2. app.engine("handlebars", engine({ defaultLayout: "main" }));
over 4 years ago · Santiago Trujillo Relatório

0

"app.engine('.hbs', exphbs({ extname: '.hbs', defaultLayout: "main"}));
                   ^
TypeError: exphbs is not a function"

This error can be resolved by adding a engine() function to the exphbs constant, OR, just calling the engine() function.

exphbs.engine(), because exphbs is a constant not a function, we have to call the engine() function from the express-handlebars package, so that we can use the handlebars templating engine!

express-handlebars usage guide


You may get a second error if you are not using a Layout (main.handlebars/ main.hbs) file.
If someone is having trouble with the main.handlebars/ main.hbs file error.
You can set the defaultLayout as null.

Something like this:

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

app.engine('handlebars', expressHbs.engine({
    layoutsDir: 'views/layouts/',
    defaultLayout: null,
    extname: 'handlebars'
  })
);

app.set('view engine', 'handlebars');
app.set('views', 'views');    // optional

layoutsDir is the location of your layouts (if there is any - this can be omitted if you are not using a layout).
extname (extension name) depends upon the choice of the user!

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda