Why is express telling me that my default view engine is not defined?

51,105

Solution 1

The source of the error describes the requirements:

if (!ext && !this.defaultEngine) throw new Error('No default engine was specified and no extension was provided.');

Express expects that you either specify the view with its extension:

res.render('index.html');

Or specify a default view engine and name your views after it:

app.set('view engine', 'ejs');

// `res.render('index')` renders `index.ejs`

Regarding your edit:

if ('function' != typeof fn) throw new Error('callback function required');

The issue is with this line:

app.engine('.html', require('ejs').renderFile());

As the documentation demonstrates, app.engine() is expecting a function reference. You can do this by simply removing the () that call renderFile:

app.engine('.html', require('ejs').renderFile);

Solution 2

need to do all the app.set and app.use in an app.configure

try this

app.configure(function(){ 
    app.set('port', process.env.PORT || 3000);
    app.set('view engine', 'ejs');
    app.engine('.html', require('ejs').renderFile());
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(path.join(__dirname, 'public')));
    // development only
    if ('development' == app.get('env')) {
        app.use(express.errorHandler());
    }
});
Share:
51,105
Christian Grabowski
Author by

Christian Grabowski

Hey there, I'm Christian. I'm a jack of all trades when it comes to programming, I write Golang, Python, Javascript, C, C++, Ruby, Rust, and a few other languages, and I do ops as well. I love coding and I love learning new things about coding.

Updated on February 21, 2020

Comments

  • Christian Grabowski
    Christian Grabowski over 4 years

    I'm using nodejs and mongodb in the back end for an app I'm working on. I'm using express to test the app, and I'm trying to use ejs to render my html files. However, I'm having the issue of my default view engine not being defined.

    Here is my app.js:

    /**
    * Module dependencies.
    */
    var express = require('express')
       , routes = require('./routes')
       , user = require('./routes/user')
       , http = require('http')
       , path = require('path');
    var conf = require('./conf');
    var app = express();
    var mongoose = require('mongoose');
       , Schema = mongoose.Schema
       , ObjectId = mongooseSchemaTypes.ObjectID;
    var UserSchema = new Schema({})
       , User;
    // all environments
    app.set('port', process.env.PORT || 3000);
    app.set('view engine', 'ejs');
    app.engine('.html', require('ejs').renderFile());
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(path.join(__dirname, 'public')));
    // development only
    if ('development' == app.get('env')) {
        app.use(express.errorHandler());
    }
    app.get('/', routes.index);
    app.get('/users', user.list);
    http.createServer(app).listen(app.get('port'), function(){
        console.log('Express server listening on port ' + app.get('port'));
    });
    

    Here is my package.json:

    {
      "name": "application-name",
      "version": "0.0.1",
      "private": true,
      "dependencies": {
    "express": "3.3.3",
    "ejs":">= 0.0.1",
    "mongoose-auth": ">= 0.0.12",
    "mongoose": ">=2.4.8",
    "everyauth": ">=0.2.28"
      }
    }
    

    ERRORS:

    Express 500 Error: Failed to lookup view "index"

    at Function.app.render (/home/christian/node_modules/nave/create/node_modules/express/lib/application.js:494:17)
    at ServerResponse.res.render (/home/christian/node_modules/nave/create/node_modules/express/lib/response.js:756:7)
    at exports.index (/home/christian/node_modules/nave/create/routes/index.js:7:7)
    at callbacks (/home/christian/node_modules/nave/create/node_modules/express/lib/router/index.js:161:37)
    at param (/home/christian/node_modules/nave/create/node_modules/express/lib/router/index.js:135:11)
    at pass (/home/christian/node_modules/nave/create/node_modules/express/lib/router/index.js:142:5)
    at Router._dispatch (/home/christian/node_modules/nave/create/node_modules/express/lib/router/index.js:170:5)
    at Object.router (/home/christian/node_modules/nave/create/node_modules/express/lib/router/index.js:33:10)
    at next (/home/christian/node_modules/nave/create/node_modules/express/node_modules/connect/lib/proto.js:190:15)
    at Object.methodOverride [as handle] (/home/christian/node_modules/nave/create/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:49:5)
    

    Now when I try to run it my terminal outputs:

    /home/christian/node_modules/nave/create/node_modules/express/lib/application.js:173
    if ('function' != typeof fn) throw new Error('callback function required');
                                     ^
    Error: callback function required
        at Function.app.engine (/home/christian/node_modules/nave/create/node_modules/express/lib/application.js:173:38)
        at Function.<anonymous> (/home/christian/node_modules/nave/create/app.js:26:9)
        at Function.app.configure (/home/christian/node_modules/nave/create/node_modules/express/lib/application.js:392:61)
        at Object.<anonymous> (/home/christian/node_modules/nave/create/app.js:23:5)
        at Module._compile (module.js:456:26)
        at Object.Module._extensions..js (module.js:474:10)
        at Module.load (module.js:356:32)
        at Function.Module._load (module.js:312:12)
        at Function.Module.runMain (module.js:497:10)
        at startup (node.js:119:16)
    

    Any help would be greatly appreciated.

  • Jonathan Lonowski
    Jonathan Lonowski almost 11 years
  • Jonathan Lonowski
    Jonathan Lonowski almost 11 years
    @ChristianGrabowski For setting a default view engine, you appear to have it in the right spot. Though, the error you added is a different issue. See my edit for that.
  • Christian Grabowski
    Christian Grabowski almost 11 years
    Yeah, that fixed it, but now express is giving me a new error that I updated above.
  • Jonathan Lonowski
    Jonathan Lonowski almost 11 years
    @ChristianGrabowski To use the default engine, the view's extension should be the engine's name. So, for app.set('view engine', 'ejs'), res.render('index') will look for index.ejs. Otherwise, you still have to specify the intended extension when rendering -- res.render('index.html').
  • Christian Grabowski
    Christian Grabowski almost 11 years
    I am not using the res.render(index) is it that it's required or is it just fine with app.engine('.html', require('ejs').renderFile)?
  • Jonathan Lonowski
    Jonathan Lonowski almost 11 years
    @ChristianGrabowski res.render('index') is just an example since you didn't include how res.render() is being used (in ./routes/index.js and ./routes/user.js). But, using app.engine('.html', ...) is fine as long as you include the extension when rendering -- res.render('index.html').
  • Christian Grabowski
    Christian Grabowski almost 11 years
    Ok, thanks for the help, but I ended up rewriting the entire app.js from scratch and it works now. I did use some of your suggestions though, so I'll give you the credit for answering.