为什么会出现“错误:找不到模块'ejs'”

时间:2019-08-18 20:14:37

标签: node.js express module ejs package.json

使用kubuntu 19.04:
我正在尝试在快速应用程序中使用ejs,但是在尝试加载本地主机时,我始终收到“错误:找不到模块'ejs'”错误。

我已经:

在项目文件夹中安装了ejs,并尝试将其全局安装到项目的父文件夹中。
Express和ejs都添加到package.json文件中(通过--save选项安装)

Ejs似乎在那里,但不起作用。

完整错误是:

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列表:

ejsdemo@1.0.0 /home/tullio/Desktop/programming/webDev/advancedExpress/EJSDemo
-ejs@2.6.2
-express@4.17.1
项目文件夹的

ls:

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

node_modules文件夹中同时存在ejs和express

实际的app.js文件:

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

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


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

和package.json文件:

    {
  "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"
  }
}

1 个答案:

答案 0 :(得分:0)

设置视图引擎

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