Nodejs/Express: Error: Failed to lookup view "error" in views directory

48,556

Solution 1

Make sure your Express App has this setup, for the current layout it sounds like you have.

// Require static assets from public folder
app.use(express.static(path.join(__dirname, 'public')));

// Set 'views' directory for any views 
// being rendered res.render()
app.set('views', path.join(__dirname, 'views'));

// Set view engine as EJS
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

It is pretty normal for views that are getting rendered by res.render() to be placed in a 'Views' directory at the top level of your app. The express-generator actually uses that view setup. You can change that by modifying the below line

// replace with the directory path below ./
app.set('views', path.join(__dirname, 'views'));

Solution 2

It seems that Express doesn't find your files, so your poor little server want to return an error, but your error file is missing in views directory.

In views directory, you have just to create a file called error.jade.

Then you have to search where Express searches your files yet.

Share:
48,556
ApathyBear
Author by

ApathyBear

Updated on May 31, 2021

Comments

  • ApathyBear
    ApathyBear almost 3 years

    I switched my nodejs template engine over to ejs (from jade). When I run my app.js with my ejs template, I receive a series of "Failed to lookup view 'error' in views" logs.

    Some of which include:

    GET /css/bootstrap.min.css 500 12.588 ms - 1390
    Error: Failed to lookup view "error" in views directory
    ...
    GET /css/clean-blog.min.css
    Error: Failed to lookup view "error" in views directory
    ...
    GET /js/bootstrap.min.js
    Error: Failed to lookup view "error" in views directory
    ...
    GET /js/jquery.js
    Error: Failed to lookup view "error" in views directory
    

    Thing is, many of these dependencies are included in the template itself (included via script tags). What is the proper place to get these to work in express? It seems like express ultimately should not be looking for these in the views folder (since they aren't views).

  • ApathyBear
    ApathyBear about 8 years
    I see. So why does express provide a public folder if it looks for these things in 'views'? These would be considered public, wouldn't they? Seems counter-intuitive.
  • peteb
    peteb about 8 years
    @ApathyBear The public folder is for static assets or for use with other front-end frameworks like Angular or React. However, you don't even need to use the public folder, you can make it whatever you like and you can declare multiple directories static. The primary motivation probably is that these views need to have the data injected and multiple templates brought together in some cases.
  • eafloresf
    eafloresf about 6 years
    This actually fixed for me, created a views/error.pug file and then express threw the actual error.
  • Plaute
    Plaute about 6 years
    Cool. I didn't check, but I think message comes from Express, so error, and problem, is the same for any view engine, Pug/Jade, Ejs and so on.
  • Kapil Raghuwanshi
    Kapil Raghuwanshi almost 4 years
    I think just 'payment' will also work. No .pug ext is needed.
  • Federico Cristina
    Federico Cristina over 3 years
    I had to change the last line: app.set('view engine', 'ejs'); in order to work
  • Shurvir Mori
    Shurvir Mori over 3 years
    @Plaute, your solution gives me 500 Server Error
  • Plaute
    Plaute over 3 years
    A file cannot result in a 500 answer code, you have another problem. Look at your server log file, these is always a response to your problems.
  • Frans
    Frans almost 3 years
    Please explain how this code solves the issue and how it's different from the accepted answer.
  • Frans
    Frans almost 3 years
    That's the same answer that @user11436097 posted. If you feel yours adds something that wasn't there, please elaborate.