Advanced Node and Express - Set up a Template Engine

hello everyone!
i faced an error that i don’t understand and i need your help to fix it please.this are the tests that i have to pass:

// running tests
Pug should be a dependency. (Test timed out)
View engine should be Pug. (Test timed out)
Use the correct ExpressJS method to render the index page from the response.
Pug should be working.
// tests completed

this is my code:
‘use strict’;
require(‘dotenv’).config();
const express = require(‘express’);
const app = express();
app.engine(‘pug’, require(‘pug’).__express);
app.set(‘views’, path.join(__dirname, ‘views’));
//app.use(express.static(path.join(__dirname, ‘public’)));
app.set(‘view engine’, ‘pug’);

app.route(‘/’).get((req, res) => {
// Change the response to render the Pug template
res.render(process.cwd() + ‘/views/pug’);
});

and this is the error:
Error: Failed to lookup view “/home/runner/boilerplate-mochachai-1/views/pug” in views directory “/home/runner/boilerplate-mochachai-1/views”

Your project link(s)

solution: boilerplate-mochachai-1 - Node.js Repl - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

1 Like

The files under views/pug are missing. Someone may have accidentally deleted them.

I recommend you just restart using a new Replit Starter and you will likely find them.

Below I left a few extra comments that may help in the next try.

// this sets the starting point of render
app.set('views', path.join(__dirname, 'views'));

// helps express to do some stuff (no idea what)
app.set('view engine', 'pug');

app.route('/').get((req, res) => {
  // so here you don't need "process.cwd()" but instead:
  res.render('pug/index.pug');  
});

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.