Node Express ejs Error: Failed to lookup view "error" in views directory Node Express ejs Error: Failed to lookup view "error" in views directory mongoose mongoose

Node Express ejs Error: Failed to lookup view "error" in views directory


You're having an error and the default express error handler is trying to display the error to the user by rendering the error view. Did you use a generator to generate your initial app? If so, did you delete the error view from the views directory?

Either change the default express error handler (likely in your app.js) so that it just spits out the raw error instead of trying to render it into a nice little view, or add the error view that it's looking for.

The error handler generated by express-cli usually looks something like this:

app.use(function(err, req, res, next) {  res.status(err.status || 500);  res.render('error', {    message: err.message,    error: err  });});

Notice it's trying to render('error' and failing to find a view named "error". What makes it an error handler is the simple fact that it accepts 4 arguments, the first being the error. Express knows that if an error is caught then it should jump down to that handler.

As far as your actual error that is causing the error handler to run, I'm not sure. You'll need to fix that so that the error shows properly and then you'll be able to debug from there.


My guess for the "headers already sent" error is that your // or just latest code is running even when one of the above if statements runs. If that's the case then you'll surely be making multiple calls to res.render or res.send. Try this:

router.get('/', function(req, res, next) {  /* ... */  //Author search  if(req.query.author !== undefined) {    /* ... */      if(authorsPosts.length==0) {        res.render(/*...*/);      } else {        res.render(/*...*/);      }    /* ... */  }  //Tag search  else if(req.query.filter !== undefined) {    /* ... */      if(taggedPosts.length==0) {        res.render(/*...*/);      } else {        res.render(/*...*/);      }    /* ... */  }  //or just latest  else {    res.render(/*...*/);  }});


I had this error, using the jsx engine though. I fixed it by making sure the file I had in my view folder was the right extension *.jsx in my case. I did have it as just index.js.