Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to lookup view in directory with Express Handlebars

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?

like image 696
bcmcfc Avatar asked Oct 19 '14 19:10

bcmcfc


People also ask

How to fix ‘error failed to lookup view ‘ in views Directory’?

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?

Why can't I view error messages in handlebars?

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.

Why is the view engine not responding to handlebars?

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:


2 Answers

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');
like image 146
bcmcfc Avatar answered Oct 05 '22 22:10

bcmcfc


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');
like image 20
SRJ Avatar answered Oct 06 '22 00:10

SRJ