Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug EJS templates in Node/Express

I'm converting PHP templates over to EJS in Node.js/Express. The template files are somewhat complex, so debugging them is not easy. Node isn't giving me any useful information for debugging, so it's making this task near impossible. Here's a sample error message.

SyntaxError: Unexpected string in "app/views/profile/index.ejs"
    at Object.Function (<anonymous>)
    at exports.compile (/Users/Gavin/Web/node_modules/ejs/lib/ejs.js:237:14)
    at Object.exports.render (/Users/Gavin/Web/node_modules/ejs/lib/ejs.js:284:10)
    at View.exports.renderFile [as engine] (/Users/Gavin/Web/node_modules/ejs/lib/ejs.js:318:20)
    at View.render (/Users/Gavin/Web/node_modules/express/lib/view.js:76:8)
    at Function.app.render (/Users/Gavin/Web/node_modules/express/lib/application.js:502:10)
    at ServerResponse.res.render (/Users/Gavin/Web/node_modules/express/lib/response.js:777:7)
    at Object.exports.profile_index [as handle] (/Users/Gavin/Web/app/controllers/routes/profile.js:72:6)
    at next_layer (/Users/Gavin/Web/node_modules/express/lib/router/route.js:103:13)
    at Object.exports.verify_user [as handle] (/Users/Gavin/Web/app/controllers/routes/account.js:305:10)

Finding the source of such a vague error message is extremely tedious. Is there an easier way to debug EJS files?

like image 467
Gavin Avatar asked Jun 20 '14 17:06

Gavin


People also ask

Which is the best template engine for Express?

There are a number of template engines available today, and the more popular ones include Pug (fka Jade), Handlebars, EJS, Mustache, Swig, and others. This post will discuss the following template engines for Express: Pug. EJS.

What is EJS template?

EJS (Embedded JavaScript Templating) is one of the most popular template engines for JavaScript. As the name suggests, it lets us embed JavaScript code in a template language that is then used to generate HTML.

How do I render an EJS file?

var http = require('http'); var ejs = require('ejs'); var server = http. createServer(function(req, res){ res. end(ejs. render('Hello World')); }); server.


1 Answers

I agree that Error Messages are vague! There are some open source applications which are making better Error handling in their applications. I use Mean.Js

Their messages are easier to understand and it actually point to a line of code and give you some hint to correct it. Its easy to work with.

I hope it helps!

like image 115
Saeed Baba Avatar answered Oct 18 '22 12:10

Saeed Baba