[英]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.