Search code examples
node.jsexpressnestjs

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?


Solution

  • 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.