Search code examples
node.jsexpresshandlebars.jsrender

nodejs handlebars: 'TypeError: this.engine is not a function'


I try to use handlebars template in Express. I get this Error-Message:

TypeError: this.engine is not a function at View.render (/home/ubuntu/workspace/node_modules/express/lib/view.js:128:8) ...

with this code:

'use strict';

var express = require('express');
var expressHandlebars  = require('express-handlebars');

var app = express();

app.engine('handlebars', expressHandlebars({defaultLayout: 'layout'}));
app.set('view engine', 'html');
app.use(express.static('views'));

app.route('/single/:id')
    .get(function (req, res) {
        res.render(process.cwd() + `/public/single-poll`, {
            id: req.params.id
        });
    });

app.listen(process.env.PORT,  function () {
    console.log('Node.js listening on port ' + process.env.PORT + '...');
});

When I replace the render() function with sendFile() is works fine. I'm aware of Express js render : TypeError: this.engine is not a function and Express js render error this.engine is not a function and TypeError: this.engine is not a function when trying to use Moustache in Express JS, but they didn't help for me.

What can be the problem?


Solution

  • Thanks to the comment from @Patrick Roberts concerning reading manuals, I rewrote it and this is how it works:

    'use strict';
    
    var express = require('express');
    var exphbs  = require('express-handlebars');
    
    var app = express();
    
    app.engine('handlebars', exphbs({defaultLayout: 'main'}));
    app.set('view engine', 'handlebars');
    
    app.route('/single/:id')
        .get(function (req, res) {
            res.render('single-poll', {
                id: req.params.id
            });
        });
    
    app.listen(process.env.PORT,  function () {
        console.log('Node.js listening on port ' + process.env.PORT + '...');
    });
    

    The main thing was, I think, changing the file structure to this:

    ├── app.js
        └── views
            ├── single-poll.handlebars
            └── layouts
                └── main.handlebars