divya dave
divya dave

Reputation: 511

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

enter image description here

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

Upvotes: 1

Views: 1388

Answers (5)

Tarun Kumar
Tarun Kumar

Reputation: 1

Simply copy this and try

const partialsPath = path.join(__dirname , "../templates/partials");  


// to set the view engine
app.set("view engine","hbs");
app.set('views', path.join(__dirname , '../templates/views'));
hbs.registerPartials(partialsPath);

Upvotes: 0

Ritesh Barapatre
Ritesh Barapatre

Reputation: 11

Simply cut your views folder and paste it inside the src. You can also write app.set('views', '../templates/views')

Upvotes: 1

Keshab Kataruka
Keshab Kataruka

Reputation: 21

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.

Upvotes: 0

Rakesh pal
Rakesh pal

Reputation: 1

instead of:

app.set('views', path.join(__dirname, viewsPath));

try:

app.set('views','viewsPath');

Upvotes: 0

shaykoo
shaykoo

Reputation: 61

Try including

app.set('view engine', 'hbs')

after express.static method.

That would help.

Upvotes: 1

Related Questions