【发布时间】:2015-08-01 00:17:44
【问题描述】:
我正在尝试做一个节点骑士示例,但我发现了一个问题。我试图遵循的例子是这样的:
var Horseman = require('node-horseman');
var horseman = new Horseman();
var numLinks = horseman
.open('http://www.google.com')
.type('input[name="q"]', 'github')
.click("button:contains('Google Search')")
.waitForNextPage()
.count("li.g");
console.log("Number of links: " + numLinks);
horseman.close();
当我制作 phantomjs example.js 时它抛出的错误是:
Error: Cannot find module 'http'
phantomjs://bootstrap.js:299 in require
phantomjs://bootstrap.js:263 in require
:3
Error: Cannot find module 'tty'
phantomjs://bootstrap.js:299 in require
phantomjs://bootstrap.js:263 in require
:6
TypeError: Object is not a constructor (evaluating 'require('debug')('horseman')')
:5
TypeError: Object is not a constructor (evaluating 'new Horseman()')
phant.js:2 in global code
我尝试在本地使用 npm install http 安装 http,但是在此之后,example/node_modules/http 上只有一个 package.json,如果我在这个位置使用 npm install,它会抛出三个警告:
- 也是核心模块的名字
- 无说明
- 没有存储库字段
关于tty,本地安装会抛出404错误。
我尝试了这个解决方案(在路径中包含 npm 文件夹)Nodejs Cannot find module,但它不起作用。
有什么建议吗??
谢谢。
编辑 不是解决方案
当我从 nodejs web 尝试这个简单的示例时,我重新安装了 node(现在我的版本是 node 0.12.3、npm 2.9.1 和 phantomjs 1.9.8):
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
如果我运行“node example.js”,它可以工作,但如果我运行“phantomjs example.js”,问题仍然存在“找不到模块 http”。
我尝试通过 npm(“npm install -g phantomjs”)安装 phantomjs,并通过在他们的网站上下载 zip、解压缩并将解压缩文件夹的路径添加到 PATH。
还有一个数据(可能会有所帮助)我的 SO 是 Windows 8.1。
重新编辑
我在我安装node的文件夹上看,node_modules上唯一的文件夹是npm,对吗?在 C:\Users\Eloy\AppData\Roaming 我有两个 npm 文件夹,其中一个是 npm-cache,另一个是 npm。最后一个的 node_modules 不包含 http 模块,npm-cache 有很多模块和 http 包括...重要吗??
谢谢。
【问题讨论】:
-
http模块是标准 node.js 安装的一部分。这听起来像您没有正确安装节点,因此节点的运行实例无法找到内置模块。 -
谢谢@friend00 但是......我想记住我运行了一个快速应用......它需要http模块,不是吗?但是,明天我会再次卸载并安装......这是正确的方法,还是有更好的方法??
-
您可以为自己编写一个非常简短的测试应用程序,该应用程序执行
var http = require('http');,然后使用该模块来查看它是否有效。在不同的操作系统上存在不同的 node.js 安装问题(例如 Windows 与 Linux 非常不同)。 -
您是否完全按照这些说明进行操作:github.com/johntitus/node-horseman#installation?
-
这可能是版本问题。你用的是哪个版本的 PhantomJS、Node、NPM、Horseman?