Error: Failed to lookup view in Express
Solution 1:
Adding to @mihai's answer:
If you are in Windows, then just concatenating __dirname' + '../public'
will result in wrong directory name (For example: c:\dev\app\module../public
).
Instead use path
, which will work irrespective of the OS:
var path = require ('path');
app.use(express.static(path.join(__dirname + '../public')));
path.join will normalize the path separator character and will return correct path value.
Solution 2:
npm install [email protected]
installs the previous version, if it helps.
I know in 3.x the view layout mechanic was removed, but this might not be your problem. Also replace express.createServer()
with express()
Update:
It's your __dirname from environment.js
It should be:
app.use(express.static(__dirname + '../public'));
Solution 3:
It is solved by adding the following code in app.js file
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname);
app.get('/', function(req, res){
res.render("index");
});
Solution 4:
I had the same error at first and i was really annoyed.
you just need to have ./
before the path to the template
res.render('./index/index');
Hope it works, worked for me.
Solution 5:
You could set the path to a constant like this and set it using express.
const viewsPath = path.join(__dirname, '../views')
app.set('view engine','hbs')
app.set('views', viewsPath)
app.get('/', function(req, res){
res.render("index");
});
This worked for me