ApathyBear
ApathyBear

Reputation: 9595

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

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).

Upvotes: 27

Views: 72532

Answers (8)

Vishal
Vishal

Reputation: 109

Remove app.use(expresslayouts). The express-ejs-layouts module is used to avoid duplication of code when there are multiple html pages. In the above code there is only one html page, so express-ejs-layouts is not required.

Once you have started using layouts in your views folder then you can use it. It won't create any problem then else in the starting you can simply avoid using expresslayouts

Upvotes: -1

Chait Sang
Chait Sang

Reputation: 1

I had my file with extension .js and I renamed it to .ejs in my view directory which I have set it up in ./src/pages

Upvotes: -2

user11436097
user11436097

Reputation: 43

Check file extension In my case, i use .js instead of .ejs

Upvotes: -1

Abimbola Bamgbelu
Abimbola Bamgbelu

Reputation: 21

I fixed mine by adding this:

app.set('views', './src/views');

Upvotes: -2

saber tabatabaee yazdi
saber tabatabaee yazdi

Reputation: 4959

its worked foe me , in my case:

i need to remove ./views/ from ./views/payment.pug

res.render('payment.pug', { title: 'Hey', message: 'Hello there!'});

this is incorrect:

res.render('./views/payment.pug', { title: 'Hey', message: 'Hello there!'});
              ^^^^^^

should remove ^^^^^^^^^ and just like first sample

Upvotes: -1

Nagnath Mungade
Nagnath Mungade

Reputation: 1009

I resolved my issue by using below code.

    // Require static assets from public folder
app.use(express.static(path.join(__dirname, 'public')));
// Set view engine as EJS
app.engine('ejs', require('ejs').renderFile);
app.set('view engine', 'ejs');
// Set 'views' directory for any views 
// being rendered res.render()
app.set('views', path.join(__dirname, ''));
app.use('/form', express.static(__dirname + '/index.html'));

Upvotes: -1

peteb
peteb

Reputation: 19418

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'));

Upvotes: 35

Plaute
Plaute

Reputation: 4889

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.

Upvotes: 5

Related Questions