[英]error: Cannot read property of undefined
I'm trying to configure my app using Express like the following way, but I'm getting errors on doing that: 我正在尝试使用Express通过以下方式配置我的应用程序,但在执行此操作时遇到错误:
server.js server.js
var express = require('express');
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'devleopment';
var app = express();
var config = require('./server/config/config')[env];
require('./server/config/express')(app, config);
app.listen(config.port);
console.log('Listening on port ' + config.port + '...');
config.js config.js
var path = require('path');
var rootPath = path.normalize(__dirname + '/../../');
module.exports = {
development: {
db: 'localhost',
rootPath: rootPath,
port: process.env.PORT || 3003
},
production: {
rootPath: rootPath,
db: 'localhost',
port: process.env.PORT || 80
}
}
But, noding it on console, I get this error message: 但是,在控制台上点头,我收到此错误消息:
app.set('views', config.rootPath + '/server/views'); ^ TypeError: Cannot read property 'rootPath' of undefined at module.exports (.\\server\\config\\expr ess.js:13:28) at Object.<anonymous> (.\\server.js:9:35 ) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:906:3 23 Jul 10:38:13 - [nodemon] app crashed - waiting for file changes before starti ng...
I think that it's comming from the initialization of rootPath. 我认为它来自rootPath的初始化。 But, I wasn't able to find what's wrong. 但是,我无法找到问题所在。 How to fix this, please? 请如何解决此问题?
Assuming you aren't setting process.env.NODE_ENV
and it's using the default value, then your problem here is a simple spelling mistake 假设您没有设置process.env.NODE_ENV
并且使用默认值,那么这里的问题是一个简单的拼写错误
process.env.NODE_ENV || 'devleopment';
should be 应该
process.env.NODE_ENV || 'development';
Your config file doesn't export an object devleopment
hence why it returns undefined
. 您的配置文件不会导出对象devleopment
因此为什么它返回undefined
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.