I'm following the base app layout you get from Express Generator and attempting to configure it for Handlebars.
Snippet:
var exphbs = require('express-handlebars');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views/'));
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
All is well until I try and change the extension for Handlebars to .hbs
as follows:
app.engine('handlebars', exphbs({defaultLayout: 'main', extname: '.hbs'}));
(and rename the files of course).
That results in:
Error: Failed to lookup view "error" in views directory
I've looked in function ExpressHandlebars(config)
in express-handlebars.js source and it does attempt to set extname apppropriately.
What am I doing wrong?
To fix ‘Error: Failed to lookup view "error" in views directory’ with Node.js and Express, we should set 'views' setting to the folder with the views. ← How to filter a complex JSON object using JavaScript? → How to get elements from a HTMLCollection with JavaScript?
You should have a View within your application currently named error.jade. Because you set up your application to use Handlebars the view engine is attempting to look for error.handlebars. You should put modify the file and that will do it. I had this issue when I upgraded the server.
Because you set up your application to use Handlebars the view engine is attempting to look for error.handlebars. You should put modify the file and that will do it. I had this issue when I upgraded the server. All I had to do is run this If the view engine is not set to jade/pug you have to use the extension right here:
Somewhat counter-intuitively, setting the extension name is not enough.
The required setup is:
app.engine('hbs', exphbs({defaultLayout: 'main', extname: '.hbs'}));
app.set('view engine', 'hbs');
Creating error.hbs file in views directory solved my issue.
var exphbs = require('express-handlebars');
// view engine setup
app.engine('hbs', exphbs({ defaultLayout: 'main', extname: '.hbs' }));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With