运行Node.js开发指南中的例子出错,目前没有头绪
发布于 12 年前 作者 papaivan 68843 次浏览 最后一次编辑是 8 年前

[xxx] express -V 3.0.0rc1

[xxx] node -v 0.8.4


我发现有个地方已经和node.js开发指南上不一样了: 在 microblog/views/下面是 index.jade layout.jade 此时我按照书中的例子生成两个 index.ejs layout.ejs放进去。 然后将原来的jade文件改名备份,再运行node app.js 直接报错:

Express

500 Error: Cannot find module ‘jade’

at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:362:17) at require (module.js:378:17) at new View (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/view.js:42:49) at Function.app.render (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/application.js:486:12) at ServerResponse.res.render (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/response.js:676:7) at exports.index (/home/xxx/Project/nodeJs/microblog/routes/index.js:7:7) at callbacks (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/router/index.js:165:11) at param (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/router/index.js:139:11)

这个问题如何引起的?思路是什么?

49 回复

Express server listening on port 3000

Error: Cannot find module 'jade’ at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:362:17) at require (module.js:378:17) at new View (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/view.js:42:49) at Function.app.render (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/application.js:486:12) at ServerResponse.res.render (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/response.js:676:7) at exports.index (/home/xxx/Project/nodeJs/microblog/routes/index.js:7:7) at callbacks (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/router/index.js:165:11) at param (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/router/index.js:139:11) GET / 500 93ms

npm install jade

刚才 npm install jade之后 运行app.js成功,但主页是express提供的原始的。

你的例子是ejs,却用了jade

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


npm install ejs

ok 主页的问题解决了。多谢WillWen

/home/xxx/Project/nodeJs/microblog/node_modules/connect-mongo/lib/connect-mongo.js:27 var Store = connect.session.Store; ^ TypeError: Cannot read property ‘Store’ of undefined at new module.exports (/home/xxx/Project/nodeJs/microblog/node_modules/connect-mongo/lib/connect-mongo.js:27:30) at Function.<anonymous> (/home/xxx/Project/nodeJs/microblog/app.js:28:12) at Function.app.configure (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/application.js:399:61) at Object.<anonymous> (/home/xxx/Project/nodeJs/microblog/app.js:17:5) at Module._compile (module.js:449:26) at Object.Module._extensions…js (module.js:467:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.runMain (module.js:492:10) at process.startup.processNextTick.process._tickCallback (node.js:244:9)

上面的问题初步解决,又有新的问题: 我已经安装了 mongoDB connect-mongo


[root@localhost microblog]# node app.js Express server listening on port 3000

/home/xxx/Project/nodeJs/microblog/node_modules/connect-mongo/lib/connect-mongo.js:126 throw new Error(‘Error connecting to database’); ^ Error: Error connecting to database at module.exports.MongoStore.proto (/home/xxx/Project/nodeJs/microblog/node_modules/connect-mongo/lib/connect-mongo.js:126:15) at Db.open (/home/xxx/Project/nodeJs/microblog/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:253:16) at Server.connect.connectionPool.on.server._serverState (/home/xxx/Project/nodeJs/microblog/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/server.js:408:7) at EventEmitter.emit (events.js:115:20) at connection.on.connectionStatus (/home/xxx/Project/nodeJs/microblog/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:93:15) at EventEmitter.emit (events.js:91:17) at Socket.errorHandler (/home/xxx/Project/nodeJs/microblog/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection.js:382:10) at Socket.EventEmitter.emit (events.js:88:17) at Socket._destroy.self.errorEmitted (net.js:335:14)

问题解决了, 我从mongodb官网下载了程序,启动数据库。

最新的错误, 貌似没有router这个方法。


[root@localhost microblog]# node app.js

/home/xxx/Project/nodeJs/microblog/app.js:32 app.use(express.router(routes)); ^ TypeError: Object function createApplication() { var app = connect(); utils.merge(app, proto); app.request = { proto: req }; app.response = { proto: res }; app.init(); return app; } has no method 'router’ at Function.<anonymous> (/home/xxx/Project/nodeJs/microblog/app.js:32:19)

/**

  • Module dependencies. */

var express = require(‘express’) , routes = require(’./routes’) , http = require(‘http’) , path = require(‘path’);

var app = express(); //var app = module.exports = express.createServer();

var MongoStore = require(‘connect-mongo’)(express); var settings = require(’./settings’);

//var app = module.exports = express.createServer(); app.configure(function(){ app.set(‘port’, process.env.PORT || 3000); app.set(‘views’, __dirname + ‘/views’); app.set(‘view engine’, ‘ejs’); app.use(express.favicon()); app.use(express.logger(‘dev’)); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.cookieParser()); app.use(express.session({ secret: settings.cookieSecret, store: new MongoStore({ db: settings.db }) })); app.use(express.router(routes)); //app.use(app.router); app.use(express.static(__dirname + ‘/public’)); });

app.configure(‘development’, function(){ app.use(express.errorHandler()); });

app.get(’/’, routes.index); app.get(’/u/:user’, routes.user); app.post(’/post’, routes.post); app.get(’/reg’, routes.reg); app.post(’/reg’, routes.doReg); app.get(’/login’, routes.login); app.post(’/login’, routes.doLogin); app.get(’/logout’, routes.logout);

http.createServer(app).listen(app.get(‘port’), function(){ console.log("Express server listening on port " + app.get(‘port’)); });

我觉得错误还是很好看的呀,, 500 Error: Cannot find module 'jade’ 明显提示是没有jade模版

@saighost app.use(app.router);可以用,但是照书里写的做用了app.use(express.router(routes)); 结果报express找不到router方法了,请问这个你知道原因么?被卡住了。。。

@saighost 好的,谢谢,确实js学得不够,甚至连怎么找错误都不懂。

后面还有无数的版本问题等着你呢!!!莫着急

app.use(express.router(routes)); //app.use(app.router);

我也是這裡卡了。。。難道是express 3版本以上沒有這個router方法了?

var MongoStore = require(‘connect-mongodb’); 加上了 app.use(express.session({ secret: settings.cookieSecret, store: new MongoStore({ db: settings.db }) }));

就会出错,如下:

TypeError: Cannot call method ‘findOne’ of null at MongoStore.MONGOSTORE.get (D:\express\microblog\node_modules\connect-mongodb\lib\connect-mongodb.js:115:15) at Object.session [as handle] (D:\express\microblog\node_modules\express\node_modules\connect\lib\middleware\session.js:298:11) at next (D:\express\microblog\node_modules\express\node_modules\connect\lib\proto.js:190:15) at Object.methodOverride [as handle] (D:\express\microblog\node_modules\express\node_modules\connect\lib\middleware\methodOverride.js:37:5) at next (D:\express\microblog\node_modules\express\node_modules\connect\lib\proto.js:190:15) at Object.cookieParser [as handle] (D:\express\microblog\node_modules\express\node_modules\connect\lib\middleware\cookieParser.js:60:5) at next (D:\express\microblog\node_modules\express\node_modules\connect\lib\proto.js:190:15) at multipart (D:\express\microblog\node_modules\express\node_modules\connect\lib\middleware\multipart.js:62:61) at module.exports (D:\express\microblog\node_modules\express\node_modules\connect\lib\middleware\bodyParser.js:57:9) at urlencoded (D:\express\microblog\node_modules\express\node_modules\connect\lib\middleware\urlencoded.js:52:72)

怎么连接mongodb

我已经暂时抛弃了书中的用法,继续进行下去, 但是使用dynamicHelper时又出错。

版本问题太多了,受不了了。

是不是这个例子在Windows XP下根本不可能运行的啊,出了一顿错误,郁闷啊

我运行 npm install 更新包依赖的时候都出一大堆错误,晕死了,是因为我是xp系统么?

npm http GET https://registry.npmjs.org/mongodb npm http GET https://registry.npmjs.org/connect-mongo npm http GET https://registry.npmjs.org/express/2.5.8 npm http GET https://registry.npmjs.org/mongodb npm http GET https://registry.npmjs.org/connect-mongo npm http GET https://registry.npmjs.org/ejs npm ERR! Error: connect ETIMEDOUT npm ERR! at errnoException (net.js:768:11) npm ERR! at Object.afterConnect [as oncomplete] (net.js:759:19) npm ERR! If you need help, you may report this log at: npm ERR! http://github.com/isaacs/npm/issues npm ERR! or email it to: npm ERR! npm-@googlegroups.com

npm ERR! System Windows_NT 5.1.2600 npm ERR! command “C:\Program Files\nodejs\\node.exe” “C:\Program Files\nod ejs\node_modules\npm\bin\npm-cli.js” "install" npm ERR! cwd E:\test\Node.js\sources\chap5\sec6\microblog npm ERR! node -v v0.8.8 npm ERR! npm -v 1.1.59 npm ERR! syscall connect npm ERR! code ETIMEDOUT npm ERR! errno ETIMEDOUT npm ERR!

请问楼主,这个现在还没解决吗?app.use(express.router(routes));

connect ETIMEDOUT 怀疑网络问题, 设置镜像吧 http://www.v2ex.com/t/15093

6F 请你你这个问题是怎么解决的了?我现在发现这个问题和你一样,能告诉一下我么?peng2e@vip.qq.com 谢谢。

//继续使用app.use(app.router),在app.js最后执行一下routes(app);即可

app.configure('development', function(){
  app.use(express.errorHandler());
});

routes(app);//这里执行

// routes/index.js 还按书上的来
module.exports=function(app){
  
  app.get('/',function(req,res){
    res.render('index',{
      title:'首页'
    });
  });
  
};

使用时请注意版本问题,并且确认依赖的modules都已经下载配置好了

dynamicHelpers lz有解决方法了吗?卡在此处。

问下,我全局跟本地都安装了express,结果Quick Start的命名在cmd里面不能运行,提示说不是内部或外部命令,怎么办??

楼主,问下你的这个问题怎么解决的

这个问题是怎么解决的。 求解 0 0

这个问题我也纠结了好久。。哈哈仔细看了下 官方文档。。 我重新下来了 新的模块解决了。。。 mongoStore = require(‘connect-mongodb’) 从来下载 connect-mongodb 多了一个 db 不知道 他俩有啥区别 。。现在不报错了。。

@jianjianfengyun mongoStore = require(‘connect-mongodb’)在后面加一个(express)就可以~~

同问卡在这里两天了

请问下在xp下的这个例子完成了,我遇到一大堆的错误,都做不下去了,express的版本问题真严重~~你要是完成了,能否发给我一份?不胜感激~

系统没有安装mongodb,使用brew install mongodb后,用终端执行代码mongod或者mongo,即可。

请问怎么解决的? 我卡了好久了

@newBingJ 擦,就是没有启动数据库

@newBingJ 我也碰到了这个问题,怎么解决?

npm install jade -d

错了 。 npm install jade --save

不要用connect-mongodb,用connect-mongo. 用法是 var MongoStore = require(‘connect-mongo’)(express);

命令已经旧了 现在用ejs搭建环境的命令是express -e ejs microblog -t会使用jade模版创建工程

貌似是这样 我做这里的时候就没有把路由分离出去了…

由于很多第三方模块式基于linux下的,有些在win下会报错误,写node还是基于linux吧,win下当做学习下就够了,如果用第三方模块要看fork或者star数。

@mapleshaw 好神奇,这是为什么。

我的出现了新问题:说是没有方法render E:\nodejs\lianxi\microblog>node app.js connect.multipart() will be removed in connect 3.0 visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives connect.limit() will be removed in connect 3.0 Express server listening on port 3000 TypeError: Object #<IncomingMessage> has no method 'render’ at E:\nodejs\lianxi\microblog\routes\index.js:8:7 at callbacks (E:\nodejs\lianxi\microblog\node_modules\express\lib\router\index.js:164:37) at param (E:\nodejs\lianxi\microblog\node_modules\express\lib\router\index.js:138:11) at pass (E:\nodejs\lianxi\microblog\node_modules\express\lib\router\index.js:145:5) at Router._dispatch (E:\nodejs\lianxi\microblog\node_modules\express\lib\router\index.js:173:5) at Object.router (E:\nodejs\lianxi\microblog\node_modules\express\lib\router\index.js:33:10) at next (E:\nodejs\lianxi\microblog\node_modules\express\node_modules\connect\lib\proto.js:193:15) at E:\nodejs\lianxi\microblog\node_modules\express\node_modules\connect\node_modules\express-session
index.js:350:9 at E:\nodejs\lianxi\microblog\node_modules\connect-mongo\lib\connect-mongo.js:222:17 at E:\nodejs\lianxi\microblog\node_modules\connect-mongo\node_modules\mongodb\lib\mongodb\collection
query.js:147:5 GET / 500 41ms

回到顶部