我如何在CasperJS中需要MongoDB?

我正尝试从CasperJS中将logging插入到MongoDB集合中,但在第一个障碍中,我被阻塞了。

我首先通过在我的应用程序的根目录下运行以下命令来安装mongodb:

npm install mongodb 

这成功完成没有任何错误。

我有一个名为server.js的CasperJS文件,其中包含以下行:

 var db = require('mongodb'); 

当我尝试运行casperjs server.js ,我得到以下错误:

 js-bson: Failed to load c++ bson extension, using pure JS version Error: Cannot find module 'util' /home/aaron/Documents/git/mateserver/phantomjs:/bootstrap.js:289 /home/aaron/Documents/git/mateserver/phantomjs:/bootstrap.js:254 in require /home/aaron/Documents/git/mateserver/node_modules/mongodb/lib/mongodb/commands/insert_command.js:2 /home/aaron/Documents/git/mateserver/node_modules/mongodb/lib/mongodb/commands/insert_command.js:162 

似乎我缺less一个包,所以我运行命令:

 npm install util 

哪个运行成功,所以我尝试再次启动我的应用程序,但得到这个错误:

 js-bson: Failed to load c++ bson extension, using pure JS version TypeError: 'undefined' is not a function (evaluating 'inherits(InsertCommand, BaseCommand)') /home/aaron/Documents/git/mateserver/node_modules/mongodb/lib/mongodb/commands/insert_command.js:38 /home/aaron/Documents/git/mateserver/node_modules/mongodb/lib/mongodb/commands/insert_command.js:162 

而这就是我卡住的地方。 我该如何解决这个问题? MongoDB与CasperJS不兼容?

我正在使用CasperJS版本1.1.0-beta3和MongoDB(通过npm安装)1.4.7版本。 主机系统是Ubuntu 14.04 64位。

CasperJS在phantomjs上运行,它具有与node.js类似但不同的标准模块。 您需要查看mongodb模块中所有require 'd模块,并重写它们以使用phantomjs API而不是node.js API 。 你可能会遇到一些无法解决的问题。

inherits函数的情况下,似乎有一个循环依赖于内部node.js util模块,无法parsing。 您可以通过在node_modules/util/node_modules/inherits中用inherits_browser.js覆盖inherits.js来解决此问题。

你可以看到SpookyJS,因为它从NodeJS驱动CasperJS