I’ve installed pug, it’s required in server.js, it’s in the dependencies, yet it refuses to work. I’ve tried “./views/pug/”, “./views/pug”, “views/pug”, etc., every variation I can think of, including a “working” snipping from an answer from this post. Same error, every time.
Error: Failed to lookup view “/views/pug/index” in views directory “/home/runner/boilerplate-advancednode/views”
…
at /home/runner/boilerplate-advancednode/server.js:18:7
…
You can leave this out (I assume that is for accessing pug’s API). I have (in a working project)
// Set view directory and view engine.
app.set('views', './views');
app.set('view engine', 'pug');
app.route('/views/:page')
.get(function(request, response) {
return response.render(request.params.page);
});
So you set the view directory and engine and then serve pages from that directory. So this will render, for instance, ./views/stories.pug when reached on the route /views/stories. Looks like you just need to set the view directory and then render pages from there; just make sure the path you set in app.set('views', directory); contains the pages you wish to render.
It’s odd though, because the “correct” solution doesn’t really match what your code is. I don’t doubt yours works, but the lesson’s correct format seems broken.
The steps from the lesson were:
Add pug as a dependency
Add app.set("view engine", "pug");
Render the index file in the views/pug directory by changing the argument in the existing res.render() to be the file path of the views/pug directory. Relative or absolute path, doesn’t need a file extension.
I tried all 3 options from the last step with many combinations between them. 3 main things I needed to do, and it didn’t seem to work.
Removing require("pug") doesn’t change the error in the least bit, so yeah I don’t think I need that.
I seem to have solved my own issue. Instead of inputting "./views/pug/index.pug", as what would make sense, I needed to enter "./pug/index.pug". Alright, whatever.
So the whole line should look like this: res.render("./pug/index.pug");