简体   繁体   English

错误:即使在 package.json 中列出,也找不到模块“ejs”

[英]Error: Cannot find module 'ejs' even though it's listed in package.json

My application is not loading the 'EJS' module when I try and invoke routing in my app.js file and I'd like to know why.当我尝试在app.js文件中调用路由时,我的应用程序没有加载“EJS”模块,我想知道原因。

My question is very similar in scope to this one , but it has some additional wrinkles to it that were not addressed in the answers there so I thought I'd turn this into a new question entirely.我的问题与这个问题的范围非常相似,但它还有一些额外的皱纹,在那里的答案中没有解决,所以我想我会把它完全变成一个新问题。

If I start a very basic app.js file like this that calls ejs it works as expected and raises no errors.如果我启动一个像这样调用ejs的非常基本的app.js文件,它会按预期工作并且不会引发任何错误。

APP.JS: APP.JS:

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

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

app.get('/', function(req, res){
    res.send("Hello World");
    }).listen(3000, function(){
    console.log("The port is now listening at 3000");
  });

However, once I modify app.js to use routes to render a view I get the error message in the title.但是,一旦我修改app.js以使用路由来呈现视图,我就会在标题中收到错误消息。

The following script provides the error:以下脚本提供了错误:

APP.JS: APP.JS:

var express = require('express');
var app = express();
var root = require('./routes/index');

app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));

app.use(root);

app.listen(3000, function(){
console.log("The port is now listening at 3000");
});

module.exports = app;

My index.js file that I use to establish the route looks like this:我用于建立路由的 index.js 文件如下所示:

INDEX.JS:索引.JS:

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res) {
res.render('index', {
  pageTitle: 'The Formula Generator',
  pageDescription: 'Easily manage and store your supplement formulas',
  pageID: 'home' });
});

module.exports = router;

EJS is correctly loaded in my package.json like so: EJS 正确加载到我的 package.json 中,如下所示:

PACKAGE.JSON:包.JSON:

"dependencies": {
"body-parser": "~1.15.2",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"ejs": "^2.5.5",
"express": "~4.14.0",
"mongodb": "^2.2.16",
"monk": "^3.1.3",
"morgan": "~1.7.0",
"serve-favicon": "~2.3.0"
 },

It's also installed in my node_modules folder as well.它也安装在我的 node_modules 文件夹中。

My file structure currently looks like this:我的文件结构目前如下所示:

在此处输入图片说明

app.js应用程序.js

var express = require('express');
var app = express();
var root = require('./routes/index');

app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));

root(app); // pass app to index

app.listen(3000, function(){
console.log("The port is now listening at 3000");
});

module.exports = app;

index.js索引.js

var controller = function(app){
    app.get('/', function(req, res) {
    res.render('index', {
      pageTitle: 'The Formula Generator',
      pageDescription: 'Easily manage and store your supplement formulas',
      pageID: 'home' });
    });
};
module.exports = controller;

package.json包.json

{
  "name": "test",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.2.6",
    "ejs": "*"
  }
}

在项目的根目录上安装 express 。

npm install express --save

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:找不到模块“../../package.json” - Error: Cannot find module '../../package.json' Openshift部署错误:找不到模块package.json - Openshift deploy error: Cannot find module package.json Google Cloud Functions 错误:“找不到模块‘sharp’”,但它在我的 package.json 中 - Google Cloud Functions error: "Cannot find module 'sharp'" but it's in my package.json npm软件包未在项目的package.json中列出,但没有无关的错误 - npm package is not listed in project's package.json but no extraneous error 电子找不到模块 package.json - electron cannot find module package.json 模块“express”未在 package.json 中列为依赖项 - Module 'express' is not listed as dependency in package.json WebStorm:模块未在 package.json 依赖项中列出 - WebStorm: Module is not listed in package.json dependencies 模块'googleapis'未在package.json中列为依赖项 - Module 'googleapis' is not listed as dependency in package.json Electron 带 Strapi,找不到模块 package.json - Electron with strapi, Cannot find module package.json 错误:找不到模块'@angular-devkit/build-angular/package.json' - Error: Cannot find module '@angular-devkit/build-angular/package.json'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM