我正尝试从 CasperJS 中将记录插入到 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
好像我少了一个包,所以我运行命令:
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
模块并重写它们以使用 phantomjs API而不是 node.js API .您可能会遇到一些无法解决的问题。
在 inherits
函数的情况下,似乎对内部 node.js util
模块存在无法解析的循环依赖。您可以通过在 node_modules/util/node_modules/inherits
中用 inherits_browser.js
覆盖 inherits.js
来解决这个问题。
关于node.js - 我如何在 CasperJS 中需要 MongoDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24736999/