Search code examples
node.jsexpressrendertypeerrorejs

TypeError: View is not a constructor EJS


Hi

I'm getting this error when I try to render an EJS page... I guess the error is on my side, so can you please help me debug?

ERROR

TypeError: View is not a constructor
at Function.render (/home/bot_king/nodelearn/project/node_modules/express/lib/application.js:570:12)
at ServerResponse.render (/home/bot_king/nodelearn/project/node_modules/express/lib/response.js:1008:7)
at /home/bot_king/nodelearn/project/app.js:16:9
at Layer.handle [as handle_request] (/home/bot_king/nodelearn/project/node_modules/express/lib/router/layer.js:95:5)
at next (/home/bot_king/nodelearn/project/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/bot_king/nodelearn/project/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/bot_king/nodelearn/project/node_modules/express/lib/router/layer.js:95:5)
at /home/bot_king/nodelearn/project/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/bot_king/nodelearn/project/node_modules/express/lib/router/index.js:335:12)
at next (/home/bot_king/nodelearn/project/node_modules/express/lib/router/index.js:275:10)

CODE

var express = require("express");
var app = express();
var port = 8080;
var ejs = require('ejs');
app.use(express.static("src/view"));

app.set("view", "./src/view");
app.set("view engine", "ejs");

app.use(express.static("public"));
app.listen(port, function(err){
console.log("The server is running on port " + port);
});

app.get("/", function(req, res){
res.render("index", { list: ["first", "2nd", "3rd"]})
});

HTML

<ul>
<% for(var i = 0; i<list.length; i++){%>
    <li> <h4> <%=list[i] %> </h4> </li>
<%}%>
</ul>

Solution

  • It should be Views not View, so you have to change the below line,

    app.set("view", "./src/view");
    

    as,

    app.set("views", "./src/view");
    

    If you still face the issue (even after making the change try removing the ./src fragment from the above statement and make it as,

    app.set("view", "./view");
    

    Hope this helps!

    See express.js settings table in documentation. There is views property.