maverick maverick - 1 year ago 316
Node.js Question

Error: callback function required

I am trying to render an object into html using node,express and mongodb. when I tried the below code

var express=require('express'),
engines = require('consolidate'),
nunjucks = require('nunjucks');

app.set('view engine', 'html');
app.engine('html', engines.nunjucks);
app.set('views', __dirname + '/views');



var server = app.listen(3000,function(){
var port=server.address().port;
console.log('Express server listening on port', port);

I am getting callback function error like below

D:\Wiki>node app.js
if ('function' != typeof fn) throw new Error('callback function required');

Error: callback function required
at (D:\Wiki\node_modules\express\lib\application.js:174:
at Object.<anonymous> (D:\Wiki\app.js:7:5)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3

can someone help me how to render html using nunjucks? Thanks in advance

Answer Source

As you can see in log - error on line 7 app.engine('html', engines.nunjucks);. So problem is here.

Add new file e.g. templateEngine.js

var nunjucks  = require('nunjucks');

module.exports = function (app) {
    // store environment env
    var env = nunjucks.configure(['views/', 'views2/'], {
        autoescape: true,
        express   : app

    // This is filter example. You can use it by {{varname | myFilter}} in template
    env.addFilter('myFilter', function(text) {
        return text.toUpperCase();

In main js call require('templateEngine')(app);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download