Reputation: 180
I am unable to use EJS as my view render engine on NodeJS.
I have found several similar questions but all state about installing EJS that really work for others. This is not same for me, may be because I am unable to choose the right directory (among so many duplicates in OpenShift repo) for the installation.
I have NodeJS default app created by OpenShift template. While installing EJS dependency I somehow screwed it and I get following error (standard NodeJS error for unavailable module):
Error: Cannot find module 'ejs' at Function._resolveFilename (module.js:337:11) at Function._load (module.js:279:25) at Module.require (module.js:359:17) at require (module.js:375:17) at View.templateEngine (/usr/lib/node_modules/express/lib/view/view.js:134:38) at Function.compile (/usr/lib/node_modules/express/lib/view.js:68:17) at ServerResponse._render (/usr/lib/node_modules/express/lib/view.js:417:18) at ServerResponse.render (/usr/lib/node_modules/express/lib/view.js:318:17) at /var/lib/openshift/5123c2494382ec16ca000222/app-root/runtime/repo/server.js:114:17 at callbacks (/usr/lib/node_modules/express/lib/router/index.js:272:11)
Besides mentioning in package.json, I tried installing ejs through terminal at app-root, runtime and nodejs-0.6 level as well (and restarting of the app), but of no use.
My directory structure is:
-app-root ---data ---repo -----node_modules (has ejs) -----server.js -----package.json ("dependencies": {"ejs" : ">=0.8.3"},) -----views -------defaultError.ejs ---runtime -----data -----node_modules (empty) -----repo (identical to app-root/repo) -------node_modules (has ejs) -nodejs-0.6 ---data ---repo (identical to app-root/repo) -----node_modules (has ejs) ---runtime -----node_modules (empty) -----repo (identical) -------node_modules (has ejs)
defaultError.ejs is just plain html. server.js has following:
self.createRoutes = function() {
self.routes = { };
//...
self.routes['/'] = function(req, res) {
res.setHeader('Content-Type', 'text/html');
res.send(self.cache_get('index.html') );
};
self.routes['/helloejs'] = function(req, res){
res.render('defaultError', { layout:false } );
};
};
self.initializeServer = function() {
self.createRoutes();
self.app = express.createServer();
self.app.set('view engine', 'ejs');
// Add handlers for the app (from the routes).
for (var r in self.routes) {
self.app.get(r, self.routes[r]);
}
};
Hope this long post makes my problem clear :)
Upvotes: 2
Views: 1165
Reputation: 2999
when install ejs you must use option --save:
npm install ejs --save
and create .gitignore file:
node_modules
Upvotes: 0
Reputation: 709
I my case, I just added ejs manually in package.json:
{
"name": "myApp"
"dependencies": {
"express": "^4.12.2",
"ejs": "^1.0.0"
}
}
And run npm install (may be you need run it with sudo) Please note, that ejs looks views directory by default
Upvotes: 0
Reputation: 48003
The error is due to using incorrect directory. You have to install packages and run your application from your project directory. Here is the directory structure of the folder i use as a project.
.
├── app.js
├── node_modules
│ ├── express
│ ├── jade
│ └── socket.io
├── package.json
├── public
│ ├── images
│ ├── javascripts
│ └── stylesheets
├── routes
│ ├── index.js
├── server.js
└── views
└── layout.jade
Using npm install
from this folder adds package folder within the node_modules sub folder (like express
, socket.io
). You should use node
from this location, as all locally installed modules are visible from this folder only.
Upvotes: 0