99

Here is my complete error:

Error: Cannot find module 'ejs'
    at Function._resolveFilename (module.js:317:11)
    at Function._load (module.js:262:25)
    at require (module.js:346:19)
    at View.templateEngine (/Users/shamoon/local/node/lib/node_modules/express/lib/view/view.js:133:38)
    at Function.compile (/Users/shamoon/local/node/lib/node_modules/express/lib/view.js:65:17)
    at ServerResponse._render (/Users/shamoon/local/node/lib/node_modules/express/lib/view.js:414:18)
    at ServerResponse.render (/Users/shamoon/local/node/lib/node_modules/express/lib/view.js:315:17)
    at /Users/shamoon/Sites/soldhere.in/app.js:26:7
    at callbacks (/Users/shamoon/local/node/lib/node_modules/express/lib/router/index.js:272:11)
    at param (/Users/shamoon/local/node/lib/node_modules/express/lib/router/index.js:246:11)

My source code is also very simple:

var express = require('express');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
  app.use(express.bodyParser());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.set('view engine', 'ejs');
app.set('view options', {
    layout: false
});

app.get('/', function(req, res) {
  res.render('index', {
    message : 'De groeten'
  });
});

app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

In my folder, I have ejs installed in node_modules which I got using npm install ejs. enter image description here so my question is.. what gives? What am I doing wrong so that node can't find EJS when I clearly have it installed?

Thanks

Shamoon
  • 41,293
  • 91
  • 306
  • 570
  • 2
    did you install express globally? if i remember correctly i had problems when installing express globally and ejs locally. try to also install express in your project folder or ejs in the global module folder. – Philipp Kyeck Oct 13 '11 at 14:11
  • I had the same problem. I ended up removing node_modules directory and doing a fresh install npm install -g. – Robert Walters Sep 15 '18 at 11:25

31 Answers31

176

I had this exact same problem a couple of days ago and couldn't figure it out. Haven't managed to fix the problem properly but this works as a temporary fix:

Go up one level (above app.js) and do npm install ejs. It will create a new node_modules folder and Express should find the module then.

evilcelery
  • 15,941
  • 8
  • 42
  • 54
  • 17
    `npm install ejs -g` might be a cleaner workaround. (`-g` for global install) – Farzher Nov 04 '12 at 23:43
  • 2
    While deploying my app to Openshift, I had the same error and noticed I forgot to add a dependency to `ejs` in `packages.json`. Now it works fine (`npm install` was done automatically during the next restart). – Bastien Jansen Mar 19 '14 at 22:15
  • 4
    I don't think you'd want to install a `node_module` globally, i've done the same before and it becomes a pain when you're deploying – pourmesomecode Oct 12 '15 at 14:45
  • 1
    I simply added the 'express' module as a dependency in my 'package.json' file and it started working. So make sure both 'ejs' and 'express' are mentioned as dependencies. – Prateek Sep 14 '16 at 06:31
  • Obviously, I upvoted the main answer(er) but I also wanted to give credit to @Stephen Bugs Kamenar for suggesting the (-g) global install. I actually cheated and did both (lazy me) and it worked immediately upon restarting my app. – Mr. Benedict Jun 24 '17 at 09:42
27

Install express locally

(npm install express while in the project's root directory)


Your project depends on both express and ejs, so you should list them both as dependencies in your package.json.

That way when you run npm install in you project directory, it'll install both express and ejs, so that var express = require('express') will be the local installation of express (which knows about the ejs module that you installed locally) rather than the global one, which doesn't.

In general it's a good idea to explicitly list all dependencies in your package.json even though some of them might already be globally installed, so you don't have these types of issues.

Community
  • 1
  • 1
Will
  • 4,241
  • 4
  • 39
  • 48
12

Way back when the same issue happened with me.

Dependency was there for ejs in JSON file, tried installing it locally and globally but did not work.

Then what I did was manually adding the module by:

app.set('view engine','ejs'); 

app.engine('ejs', require('ejs').__express);

Then it works.

Eduardo Baitello
  • 10,469
  • 7
  • 46
  • 74
Asad Ashraf
  • 1,425
  • 8
  • 19
  • this is especially true when you have express as a dependency in submodule – user210504 May 11 '20 at 19:13
  • This makes it [ejs] discoverable when compiling (or simply bundling if not using typescript) your server to via webpack. (either copying over views to dist or further work is required to get it working; however.) – Daniel Dec 04 '20 at 15:24
  • Error: Cannot find module 'ejs' – fdrv Feb 07 '23 at 04:47
10

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

levon
  • 689
  • 6
  • 13
9

I had the same issue. Once I set environment variable NODE_PATH to the location of my modules (/usr/local/node-v0.8.4/node_modules in my case) the problem went away. P.S. NODE_PATH accepts a colon separated list of directories if you need to specify more than one.

Pete
  • 409
  • 4
  • 8
6

I installed ejs using command npm install ejs in express directory level and this solved my problem.

i have install express using steps mention in express guide http://expressjs.com/guide.html

Shankar BS
  • 8,394
  • 6
  • 41
  • 53
Ramesh Kotkar
  • 747
  • 9
  • 11
6

After installing node express project you need to install the ejs or pug package for that

either you can try with node

npm install ejs 
npm install pug

or you can try with yarn

yarn add ejs
yarn add pug

globally or locally

sanjay
  • 695
  • 11
  • 22
4

Install it locally rather installing it globally. Then your project may be run on any machine without any error.I think its better.

npm install express --save
npm install ejs --save
Chanaka Fernando
  • 2,176
  • 19
  • 19
4

After you've installed Express V x.x.x You need to choose an template view-engine. There are many really easy to learn. My personal go-to is EJS.

Other really great and easy to learn could be:

To install EJS (And fix your error) Run in root of your project:

npm install ejs

Or if you're using Yarn:

yarn add ejs

Next you'll need to require the module, so open up your file where you require express (usually app.js or server.js)

add below var express = require('express');

var ejs = require('ejs');
Timm Jensen
  • 180
  • 12
4

i had the same problem. So i did the following and it worked for me.

solution:

  1. run " npm init " in the project directory if not already done.
  2. install ejs and express as follows:

npm install ejs --save
npm install express --save

by doing so it creates the required dependencies in the package.json file

Hemanta
  • 41
  • 1
3

I had this problem. I debugged using node-inspector and saw that from the node_modules folder where the express source files were, ejs was not installed. So I installed it there and it worked.

npm install -g ejs didn't put it where I expected it to despite NODE_PATH being set to the same node_modules folder. Prob doing it wrong, just started with node.

Typo Johnson
  • 5,974
  • 6
  • 29
  • 40
3

I installed both: express and ejs with the option --save:

npm install ejs --save npm install express --save

This way express and ejs are dependecies package.json file.

colo
  • 152
  • 4
3

Reinstalling npm, express and ejs fixed my problem

This one worked for me,

  1. On your terminal or cmd -> Go to your apps directory,
  2. cd pathtoyourapp/AppName
  3. rerun your 'npm install'
  4. rerun your 'npm install express'
  5. rerun your 'npm install ejs'

after that, the error was fixed.

apelidoko
  • 782
  • 1
  • 7
  • 23
2

STEP 1

See npm ls | grep ejs at root level of your project to check if you have already added ejs dependency to your project.

If not, add it as dependencies to your project. (I prefer adding dependency to package.json instead of npm installing the module.)

eg.

{                                                                                                      
  "name": "musicpedia",                                                                                
  "version": "0.0.0",                                                                                  
  "private": true,                                                                                     
  "scripts": {                                                                                         
    "start": "node ./bin/www"                                                                          
  },                                                                                                   
  "dependencies": {                                                                                    
    "body-parser": "~1.15.1",                                                                          
    "cookie-parser": "~1.4.3",                                                                         
    "debug": "~2.2.0",                                                                                 
    "express": "~4.13.4",                                                                              
    "jade": "~1.11.0",                                                                                 
    "ejs": "^1.0.0",                                                                                                                                                            
    "morgan": "~1.7.0",                                                                                
    "serve-favicon": "~2.3.0"                                                                          
  }                                                                                                    
}   

STEP 2 download the dependencies

npm install

STEP 3 check ejs module

$ npm ls | grep ejs
musicpedia@0.0.0 /Users/prayagupd/nodejs-fkers/musicpedia
├── ejs@1.0.0
prayagupa
  • 30,204
  • 14
  • 155
  • 192
  • Great! Did all the above, but didn't run npm install again. Thanks. – N. Osil Aug 02 '17 at 18:09
  • are you saying `npm install` did not work after adding `"ejs": "^1.0.0"`? If so, remove that line and install another way as `npm install ejs` - https://www.npmjs.com/package/ejs. It should add a `ejs: latest version` to package.json – prayagupa Aug 02 '17 at 18:14
2

Add dependency in package.json and then run npm install

    {
  ...
  ... 
  "dependencies": {
    "express": "*",
    "ejs": "*",
  }
}
marwari
  • 181
  • 2
  • 13
2

I think ejs template engine is not properly installed on your machine. You just install the template engine using npm

npm install ejs --save

then include the following code in app.js

app.set('view engine', 'ejs')
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
1

I have the same issue it resolve after installing the express in my project directory. previously i install it in global scope with -g option with npm install command.

Mohit
  • 191
  • 1
  • 12
1

In my case it was a stupid mistake- it was a typo in the middleware. I wrote app.set('view engine', 'ejs.'); the dot caused the error. I installed ejs and express locally

1

In my case there was no silly syntax error, but same error arised. I had installed ejs and ejs-mate globally. I installed it locally and found my error resolved.

1

kindly ensure that your dependencies in your package.json files are up to date. Try reinstalling them one at a time after also ensuring that your NPM is the latest version (up-to-date). It worked for me. I advise you to run npm install for the packages(thats what worked in my own case after it refused to work because I added the dependencies manually).

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
1

Ensure all dependencies are installed. npm install

I was making a quick app for myself and I had forgotten to add express. Threw the above error.

1

I face same error for ejs, then i just run node install ejs This will install ejs again.

and then also run npm install to install node_modules again. That's work for me.

akshay_sushir
  • 1,483
  • 11
  • 9
1

I ran into this problem after I forgot to install ejs before the first time I ran my app. For some reason ejs was not being seen after installing it later. I found a quick, clean, and effective solution to this problem which was to reinstall express by running npm uninstall express then npm install express in the local directory before restarting the server.

yohosuff
  • 1,359
  • 1
  • 11
  • 19
1

npm i ejs --force

this worked for me

1

Installing express locally solved my same problem. npm i express --save

1

i had the same issue and tried a few of the given solutions but it still didn't work. all i did was to run the "npx yarn" command in the root folder of my project and that was it.

AWESOME WORLD
  • 109
  • 1
  • 7
1

you must have

const ejs = require('ejs')
app.set('view engine', 'ejs')
0
app.set('view engine', 'ejs')

and then in the terminal

npm install ejs --save 

resolves the issue

Hom Bahrani
  • 3,022
  • 27
  • 25
0

In my case, I just uninstall then install ejs again.

npm uninstall ejs

then

npm install ejs
Ian Natividad
  • 83
  • 1
  • 2
  • 6
0

npm install ejs --save worked for me ! ✅

On goormIDE, I had this file configuration :

  • container
    • main.js
    • package-lock.json
    • package.json
    • node_modules
    • views
      • home.ejs

In my main.js file, I also had this route

app.get("/", function(req, res){
res.render("home.ejs");
})

npm install ejs -g didn't add the corresponding dependency within the package.json. npm install ejs --save did. I executed the command line from the container directory. Manually it could have been added into the package.json with : **

"dependencies": {
    "ejs": "^3.0.2",}

**

Tristan Bochu
  • 51
  • 1
  • 1
0

I had this same issue and I tried every command in the answers and everywhere else and nothing worked. Eventually, I went to another project of mine, copied the ejs folder from node_modules, then pasted it in my current broken project, after that everything worked.

It's worth noting that I looked in my node_modules/ejs folder before and I couldn't find any files other than a package.json, license, etc., no lib folder, or any .js files and running yarn install, yarn remove ejs/yarn add ejs/yarn add ejs --save did nothing to fix it.

Yuniac
  • 343
  • 3
  • 11