'Failed to lookup view "index" in views directory in express
Learning express in the meantime I am unable to solve this error which says Failed to lookup view "index" in views directory while my folder structure looks like this
I tried setting the path nothing works
const pathDirectory = path.join(__dirname, '../public')
const viewsPath = path.join(__dirname, '../templates/views')
const partialsPath = path.join(__dirname, '../templates/partials')
app.set('views', path.join(__dirname, viewsPath));
hbs.registerPartials(partialsPath)
app.use(express.static(pathDirectory))
The page of index.hbs should be displayed
Solution 1:[1]
Try including
app.set('view engine', 'hbs')
after express.static method.
That would help.
Solution 2:[2]
instead of:
app.set('views', path.join(__dirname, viewsPath));
try:
app.set('views','viewsPath');
Solution 3:[3]
While you run it on the terminal change your directory to webserver, then run the app.js file using
node src/app.js
I tried this and it worked for me.
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 | CKE |
| Solution 2 | Grayrigel |
| Solution 3 | Keshab Kataruka |

