Here is my complete error:
Error: Cannot find module \'ejs\'
at Function._resolveFilename (module.js:317:11)
at Function._load (module.js:262:25)
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).
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');
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.
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
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.
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.