question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to set default directory for views

See original GitHub issue

Hello, I am unable to set default views directory.

OS: Ubuntu 15.04 Node: 4.0.0

I am in app directory (…). When running node server/server.js, it ignores app.set('views', path.join(__dirname, 'views')); from server.js and tries to load .../views/layouts/main.handlebars instead .../server/views/layouts/main.handlebars

.
└── server
    ├── server.js
    └── views
        ├── home.handlebars
        └── layouts
            └── main.handlebars

server.js

'use strict';

var path = require('path');
var express = require('express');
var exphbs  = require('express-handlebars');
var app = express();

// Rendering engine setup
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.set('views', path.join(__dirname, 'views'));
console.log(path.join(__dirname, 'views'));

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

app.listen(8888);

EDIT: it works when i am in (.../server/ directory) and node server.js, it is wierd, becouse it should get right path from app.locals.setting.views variable.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:5
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

56reactions
lmarancommented, Nov 25, 2015

@camabeh - I have exactly the same issue. I modified (temporary) the code in the library according to your PR changes and confirm that it solves the problem. Until your PR is merged I choose to set layoutsDir and partialsDir in engine factory:

app.set('views', config.root + '/server/views');

var exphbs = require('express-handlebars');
app.engine('.hbs', exphbs({
        defaultLayout: 'main', 
        extname: '.hbs',
        layoutsDir:'server/views/layouts',
        partialsDir:'server/views/partials'
}));
app.set('view engine', '.hbs');
0reactions
nathandelgado-devcommented, Jan 10, 2022

Thanks Bro for this!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set handlebars layout directory? - Stack Overflow
You need to point the directories while creating an instance of it. const hbs = exphbs.create({ extname :'hbs', layoutsDir : 'path/to/layout ...
Read more >
How to change default view in Windows Folder Explorer
How to change default view in Windows Folder Explorer ; a. · Click on Organize menu and click on click Folder and Search...
Read more >
How to set a default Folder View for all folders in Windows 11/10
There are mainly two methods. First, you can right-click after opening a folder and select the Properties option.
Read more >
How to Set a Default Folder View for All Folders in Windows ...
How to Set a Default Folder View for All Folders in Windows 11/10In Windows File Explorer, ... Your browser can't play this video....
Read more >
How to Fix Set Default Location Button Not Working ... - YouTube
When you click on this default location option, it does nothing on Windows 11. Even if you enable the location and let apps...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found