Express 4.0 Failed to Look up view in "/views" directory

603 views Asked by At

I am working with Express 4.0 and Express3-handlebars libraries for NodeJS.

Here is the configuration

app.set('views', path.join(__dirname, 'views/'));
app.engine('hbs', hbs({defaultLayout: 'main', extname: '.hbs'}));
app.set('view engine', 'hbs');

The contact.html is in the views directory

app.get('/', function(req, res) {
 res.render("contact");
});

This is the error i get :

Error: Failed to lookup view "contact" in views directory "/Users/max23/Desktop/Node/views/"

I spent over an hour trying to fix it to no avail. What is Wrong with the code?

3

There are 3 answers

1
Bidhan On BEST ANSWER

You're using Handlebars templating library, so your views should have .hbs extension, not .html. Change contact.html to contact.hbs and do that to all of your templates. Also, change this line

app.engine('hbs', hbs({defaultLayout: 'main.hbs', extname: '.hbs'}));
0
CyborgFish On

As you are using hbs as your view system, you should use the .hbs extension for your view files. It fails to lookup the view because its an HTML, and its looking for contact.hbs

I personally dont have much experience with the handlebars library, but I suggest you change the contact.html extension to .hbs and try again

0
sam100rav On

There is something wrong in the setting views path. Try changing the line

app.set('views', path.join(__dirname, 'views/'));

by

app.set('views', path.join(__dirname, '/views'));

If not solved, show your folder structure.