Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No such file or directory #195

Open
itotallyrock opened this issue Oct 28, 2016 · 13 comments
Open

No such file or directory #195

itotallyrock opened this issue Oct 28, 2016 · 13 comments

Comments

@itotallyrock
Copy link

I'm running into the same issue and it involves the differences in operating systems. This is because the application works as intended on windows, but fails on linux with
Error: ENOENT: no such file or directory, open '/views/layouts/main.handlebars'

Here is my file structure.

|-- index.js
|-- package.json
|-- public
|   |-- css
|   |   `-- global.css
|   |-- img
|   `-- js
|       `-- main.js
`-- views
    |-- 404.handlebars
    |-- index.handlebars
    |-- layouts
    |   `-- main.handlebars
    `-- partials
        |-- footer.handlebars
        |-- nav.handlebars
        `-- title.handlebars

and here is part of the index.js file

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

While I was making this issue, I ran into another error
Error: Failed to lookup view "index" in views directory "/views"
Which contains no reference back to express-handlebars but only mentions express in the stack.

I'm assuming this is just a simple mistake on my part but it might be worth documenting for people like me who don't quite understand the subtle differences between linux and windows.

@sgnl
Copy link

sgnl commented Jan 19, 2017

Try using path.resolve instead of path.join and see if it helps.

@kas
Copy link

kas commented Apr 11, 2017

I am also experiencing this issue. I use app.set('views', __dirname + '/views'); but the ENOENT error says it is trying to access project-name/views/layouts/main.handlebars instead of the expected project-name/src/views/layouts/main.handlebars'.

@ndeufemia
Copy link

I ran into the same issue, and could solve it by setting the layoutDir and partialsDir option as follows:

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

@DmitryMyadzelets
Copy link

How to reproduce this issue:

  • Make sure you have a layout in the views/layouts directory.
  • Run project from a directory different to the app's directory: node path-to-app.

From this discussion it seems that the bug is due to pure default path resolving in express-handlebars.

This issue is fixed by this PR but not published yet.

@erickhora
Copy link

For anyone out there who doesn't have an layouts or layout folder and is having the same issue. Try this in the rendering of the page:
res.render('index', {layout: false});

@johnnyodonnell
Copy link

Is there a way to set the set the default value of layout to false. I'm hoping to avoid updating all of my render functions from something like:

res.render("index");

to

res.render("index", {layout: false});

@UziTech
Copy link

UziTech commented Jul 18, 2019

defaultLayout: null should work

@knoxcard
Copy link
Contributor

knoxcard commented Aug 5, 2019

@johnnyodonnell

app.locals.layout = false

@ahmedatef1610
Copy link

const expressHandlebars  = require('express-handlebars');

app.engine('hbs', expressHandlebars({
    defaultLayout: '',
}));

app.set('view engine', 'hbs');
app.set('views', 'views');

@pulkit5ingh
Copy link

For anyone out there who doesn't have an layouts or layout folder and is having the same issue. Try this in the rendering of the page:
res.render('index', {layout: false});

Thanks Its Working...

@Alpha4733
Copy link

For anyone out there who doesn't have an layouts or layout folder and is having the same issue. Try this in the rendering of the page:
res.render('index', {layout: false});

Thanks Its Working...

For anyone out there who doesn't have an layouts or layout folder and is having the same issue. Try this in the rendering of the page:
res.render('index', {layout: false});

Thanks Its Working...

but what if we have layout but still showing such problem

@muhammadwarisali
Copy link

muhammadwarisali commented Apr 10, 2020

This solved my issue:

app.engine('hbs',handlebars({ 
    layoutsDir: path.join(__dirname,'views','layouts'), 
    defaultLayout: 'main-layout', 
    extname: 'hbs'
}))

@forcetrekker
Copy link

I had a silly mistake as:
extName: '.hbs'
instead of
extname: '.hbs'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests