Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to lookup view "index" in views directory NestJs

I am getting the following error from nestjs:

Failed to lookup view "index" in views directory "/api/dist/views"

I am trying to use handlebars templating engine with NestJs. I have followed the NestJs documentation here directly without changing a thing. For some weird reason i still get the same error.

I have even created a fresh project using the nestjs cli, followed the direction in the documentation above and still getting thesame error.

I have also followed the help in this stackoverflow answer here and added "assets": ["**/*.hbs"] to my nest-cli.json at the root of the project. Still getting thesame error.

Can anyone help me please? or Has anyone experienced this or is it just me?

like image 261
Daniel Adigun Avatar asked Dec 23 '22 18:12

Daniel Adigun


1 Answers

Found a solution that helped me. Hope this saves a dev's life:

First

move public and views folder into your src folder

Next in your main.ts file

import {resolve } from 'path';

Then

change this :

app.useStaticAssets(join(__dirname, '..', 'public'));
app.setBaseViewsDir(join(__dirname, '..', 'views'));
app.setViewEngine('hbs');

to this :

 app.useStaticAssets(resolve('./src/public'));
 app.setBaseViewsDir(resolve('./src/views'));
 app.setViewEngine('hbs');

you are good to go.

like image 58
Daniel Adigun Avatar answered Dec 26 '22 01:12

Daniel Adigun