Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJS errors: not defined; can't find module 'undefined'; can't find module 'ejs'; body is not defined

I've been very frustrated with the "Let's build twitter" node app at Chapter 2 in the O'Reilly book "Node.js Up And Running".

I've never used EJS and don't even know what extension to put for EJS files. Plus, I can't get my app working getting these various errors:

  • partial is not defined
  • Cannot find module 'undefined' where doing app.render('index'...
  • Cannot find module 'ejs'
  • body is not defined

It's even more frustrating since it's only the Chapter 2 and I'm wondering if it wouldn't be preferable to switch to another material...

like image 416
Tinou Avatar asked Jul 29 '12 22:07

Tinou


2 Answers

If you too have been frustrated with the "Let's build twitter" programming tutorial at the Chapter 2 in the O'Reilly Up and running book, here is the complement to make this "app" work.

Pre-requisites:

  1. For this app to work, make sure you have a version of Express < 3.X. In fact, in version 3.0 and higher they removed the "partial" support and now is template specific. So make sure to install a version 2.x by doing so: npm install [email protected]
  2. The book doesn't provide any information on how to use EJS files. After a bit of research, in order for express to understand and parse EJS file you have to install...ejs. The installation is pretty straightforward like any other module: npm install ejs.

The meat:

  1. For this app, all the files in the folder views and partials should have the extension .ejs
  2. You installed EJS but you have to tell express to use EJS as the template format in the app.render() function You have two ways to do it:

(1) You set EJS as the default template engine and then just tell express to render your file app.set('view engine', 'ejs');

res.render('index', ...)

(2) You just tell to the app.render() function to use EJS, express will take care of it

res.render('index.ejs', ...)

If at that point it doesn't work or it's still not clear, nothing is better than looking at working code. Fork or download the app here.

Hope it helps other readers.

like image 182
Tinou Avatar answered Oct 06 '22 20:10

Tinou


Support for EJS has been removed from Express v. 3. I've built another example using Express 3 and Jade templates:

https://github.com/nosolopau/node-up-and-running-chirpie-express-3

like image 32
Pablo Torrecilla Avatar answered Oct 06 '22 19:10

Pablo Torrecilla