express-handlebars layouts not using "views" setting?

Created on 12 Jun 2015  Â·  11Comments  Â·  Source: expressjs/express

Hi.

I am a bit puzzled by this, as there are no reports, yet my various attempts came up with nothing.

Anyway - trying to relocate the views folder doesn't work.

This code (in server.js file in the root of the project):

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

var app = express();

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

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

app.listen(8080);

where views1 is (and it is in the root folder, the same with the server.js above):

views1                         
└───layouts     
│        └───main.hbs
└───index.hbs   

produces this (abridged):

Error: ENOENT, open '<path-to-root-folder>/views/layouts/main.hbs'

Renaming the folder to views and removing app.set('views', ...); line makes this work properly.

I must be missing something, as this is too basic a functionality to not work.

P.S.

There were additional attempts revolving around using something like this instead (to make sure that the path is resolved properly and whatnot):

app.set('views', path.resolve(__dirname, 'views1'));

to no avail.

4.x question

Most helpful comment

Looks like you need to specify the layoutsDir option, for express-handlebars to resolve layout files correctly (at least, that's what I'm getting from reading https://github.com/ericf/express-handlebars/tree/v2.0.1#layoutsdirviewslayouts):

app.engine('hbs', expressHbs({extname:'hbs', defaultLayout:'main.hbs', layoutsDir: __dirname + '/views1/layouts'}));

All 11 comments

Hi! Can you start your app with the following command, and paste what is printed on the command line here?

DEBUG=express:application node server.js

Ehh...... probably won't do you any good, but...

  express:application compile etag weak +0ms
  express:application compile query parser extended +0ms
  express:application compile trust proxy false +0ms
  express:application booting in development mode +0ms
Listening on http://localhost:8080
GET / 500 44.299 ms - 148
Error: ENOENT, open '<path-to-root-folder>\views\layouts\main.hbs'
    at Error (native)

Note I removed the path and replaced it with <path-to-root-folder> due to various reasons.

EDIT: the port moved to 8080. Updated my earlier example as well.

Thanks! I forgot that we are not actually printing all the settings in the debug output (bug! :) ) so that doesn't seem to help. I'll have to try it out, but in the end, the views module you are using, express-handlebars, is ultimately responsible for honoring that setting. If I have time to debug, I'll try to see where it's getting lost, but I suspect it's happening in the express-handlebars module.

I fail to see how is it responsible, since changing the name to views works just fine.

But thanks for trying it out.

Gotcha. Yea, I'm not sure, but I will try to investigate later :) If you do fine the issue in Express, a PR would be welcome!

I fail to see how is it responsible, since changing the name to views works just fine.

This is because we only resolve the index file in your example; your error is failing to resolve main.hbs, which the way that is resolved is done purely by express-handlebars module, as layouts are not a part of Express and implemented by the view engines (which is why you had to give that option to their constructor).

Ah, I see. Lemme try this with jade

Looks like you need to specify the layoutsDir option, for express-handlebars to resolve layout files correctly (at least, that's what I'm getting from reading https://github.com/ericf/express-handlebars/tree/v2.0.1#layoutsdirviewslayouts):

app.engine('hbs', expressHbs({extname:'hbs', defaultLayout:'main.hbs', layoutsDir: __dirname + '/views1/layouts'}));

Yep. My bad. :dizzy_face: ... what's the _embarrassed_ emoji name?

lol. I think it's U+1F633 FLUSHED FACE 😳

I'm going to close this, but it may be worth making a bug over at express-handlebars (or I guess even a PR), because we do provide what the views dir is to the view engine, so the module could default to expressViewsDir + '/layouts' instead of just 'views/layouts'.

Will move it over there, sure. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

extensionsapp picture extensionsapp  Â·  3Comments

despairblue picture despairblue  Â·  3Comments

guyisra picture guyisra  Â·  3Comments

HafidAbnaou picture HafidAbnaou  Â·  3Comments

ZeddYu picture ZeddYu  Â·  3Comments