The engine can't recognise .hbs extension with partials
Env settings:
node 10.15.1
npm 6.4.1
handlebars 4.1.2
express 4.16.4
Just updated the express-handlebars from 3.0.0 to 3.1.0 run into this issue: server.js
const exphbs = require('express-handlebars');
const hbsHelpers = require('./config/hbsHelpers');
const hbs = exphbs.create({
extName: '.hbs',
partialsDir: 'views/partials/',
layoutsDir: 'views/layouts/',
defaultLayout: 'main.hbs',
helpers: hbsHelpers,
});
app.set('views', path.join(__dirname, 'views'));
app.engine('.hbs', hbs.engine);
app.set('view engine', '.hbs');
app.get('/', function (req, res) {
res.render('test.hbs');
})
The extName is defined in the config.
in test.hbs
<div class="wrapper home">
<h1>Test Page</h1>
{{> pages/nav }}
</div>
File structure
.
├── app.js
└── views
├── test.hbs
└── partials
└── pages
└──nav.hbs
This results in error
Error: The partial pages/nav could not be found
But if I change the nav.hbs to nav.handlebars, the error is gone.
nav.hbs used to be working fine before I update the express-handlebars to 3.1.0
just want to throw in here that the handlebars engine wasn't recognizing partials with .hbs until I specified it in the value passed to app.engine, even though templates were recognized just fine.
I passed in {extname: 'hbs'} to the engine factory function and it started picking up partials with an .hbs extension. This worked for me with and without a leading dot, i.e. using hbs and .hbs as the value for extname worked the same.
const express = require('express');
const hbs = require('express-handlebars');
const app = express();
// app.engine('hbs', hbs());
app.engine('hbs', hbs({ extname: 'hbs' ));
app.set('view engine', 'hbs');
const express = require('express'); const exphbs = require('express-handlebars'); const path = require('path'); const router = require('./routes');
const app = express();
// habilitar handlebarss como view
app.engine('.hbs', exphbs.engine({ defaultLayout: 'layout' }) );
