简体   繁体   中英

Why do I get "Error: Cannot find module 'ejs '"

Using kubuntu 19.04:
I'm trying to use ejs in a express app but I keep gettin the "Error: Cannot find module 'ejs '" error as I try to load the localhost.

I've already:

Installed ejs in the project folder, tried to install it globally and in the parent folder of the project.
Added to the package.json file both express and ejs ( installed with the --save option)

Ejs seems to be there but does not work.

the full error is:

Error: Cannot find module 'ejs '
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at new View (/home/tullio/Desktop/programming/webDev/advancedExpress/EJSDemo/node_modules/express/lib/view.js:81:14)
at Function.render (/home/tullio/Desktop/programming/webDev/advancedExpress/EJSDemo/node_modules/express/lib/application.js:570:12)
at ServerResponse.render (/home/tullio/Desktop/programming/webDev/advancedExpress/EJSDemo/node_modules/express/lib/response.js:1012:7)
at /home/tullio/Desktop/programming/webDev/advancedExpress/EJSDemo/app.js:5:7
at Layer.handle [as handle_request] (/home/tullio/Desktop/programming/webDev/advancedExpress/EJSDemo/node_modules/express/lib/router/layer.js:95:5)
at next (/home/tullio/Desktop/programming/webDev/advancedExpress/EJSDemo/node_modules/express/lib/router/route.js:137:13)

npm list :

ejsdemo@1.0.0 /home/tullio/Desktop/programming/webDev/advancedExpress/EJSDemo
-ejs@2.6.2
-express@4.17.1

ls of the project folder:

total 32
-rw-rw-r--  1 tullio tullio   185 ago 17 18:20 app.js
drwxrwxr-x 53 tullio tullio  4096 ago 18 21:43 node_modules
-rw-rw-r--  1 tullio tullio   285 ago 18 19:42 package.json
-rw-rw-r--  1 tullio tullio 14459 ago 18 19:42 package-lock.json
drwxrwxr-x  2 tullio tullio  4096 ago 18 19:41 views

Both ejs and express are present in the node_modules folder

Actual app.js file:

var express = require("express");
var app = express();

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


app.listen(3000, function(){
  console.log("Server Online")
});

and package.json file:

    {
  "name": "ejsdemo",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Tullio Angius",
  "license": "ISC",
  "dependencies": {
    "ejs": "^2.6.2",
    "express": "^4.17.1"
  }
}

Set view engine

const express = require("express");
const app = express();

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

// index page 
app.get('/', function(req, res){
  res.render('home')// or whatever is your path to view without extension
});

...

Setting up view engine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM