Sometimes, we want to fix ‘Error: Failed to lookup view "error" in views directory’ with Node.js and Express.
In this article, we’ll look at how to fix ‘Error: Failed to lookup view "error" in views directory’ with Node.js and Express.
For instance, we write:
const express = require('express')
const app = express()
const port = 3000
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
to set the location of the view files with
app.set('views', path.join(__dirname, 'views'));
We may also set the static asset folder location with
app.use(express.static(path.join(__dirname, 'public')));
And we set the template engine to render the views with
app.engine('html', require('ejs').renderFile);
Conclusion
To fix ‘Error: Failed to lookup view "error" in views directory’ with Node.js and Express, we should set 'views'
setting to the folder with the views.