Tinou
Tinou

Reputation: 6158

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:

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...

Upvotes: 5

Views: 6134

Answers (6)

Surendra Babu Parchuru
Surendra Babu Parchuru

Reputation: 1012

Install express@4 in the current project directory, it solved my problem!

Upvotes: 1

N.Y. Gudlanur
N.Y. Gudlanur

Reputation: 125

I have installed express@4 and resolved issue. if we write any code out side the editor and it will shows that error.

Regards, Ningappa

Upvotes: -1

Adrian Statescu
Adrian Statescu

Reputation: 99

O'Reilly Book Node.js Up and Running - Part 1 => Chapter 2 Doing Interesting Things => Let's Build Twitter (working code for Express 3.0 and ejs embedded JavaScript for node renderer without partials) download source: https://github.com/thinkphp/express-tweets

Upvotes: 0

Pablo Torrecilla
Pablo Torrecilla

Reputation: 2108

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

Upvotes: 2

user1265936
user1265936

Reputation: 1

I was also frustrated so I built a working example - available here for download:

https://github.com/iotaweb/node-up-and-running-chirpie

Upvotes: 0

Tinou
Tinou

Reputation: 6158

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.

Upvotes: 10

Related Questions