'Failed to lookup view "index" in views directory
I am trying to learn node.js.
I have the following code.
var express = require('express');
var app = express();
var port = process.env.PORT || 5000;
app.use(express.static('public'));
app.use(express.static('src/views'));
app.set('view engine', 'ejs');
app.get('/', function(req, res) {
res.render('index', { title: 'Hello from render', nav: ['Books', 'Author'] });
});
Failed to lookup view "index" in views directory I have the file named index.ejs. How can I get rid of this error?
Solution 1:[1]
Try with
app.set('views', __dirname + '/views');
Solution 2:[2]
This is how I fixed it.
app.set('views', './src/views');
app.set('view engine', 'ejs');
Solution 3:[3]
I had the same problem too. I have saved the index file as .html file. But it should be index.ejs. then it worked. Check that too
Solution 4:[4]
For me it was a banal problem: when I named my 'views' folder, accidentally I typed a white space before the name, so the name was ' veiws', thus, ejs couldn't find the 'views' folder.
Solution 5:[5]
i fixed this error by writing below line befor my app.set('viewengine' , 'ejs')
app.set('views',path.join(__dirname,'views'));
using path is more convenient ,also note to require path befor using it
Solution 6:[6]
I had the same issue, then I saved my index file inside the views folder. Now the issue is gone. Also add this line of code in your file
app.set('views', path.join(__dirname, '/views'))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Oscar |
Solution 2 | user2281858 |
Solution 3 | maneesha |
Solution 4 | Ador |
Solution 5 | |
Solution 6 | 0xLogN |