使用来自 casperjs 的节点模块 [英] Use a node module from casperjs
问题描述
是否可以安装一个节点模块,通过 npm 安装,然后从 casperjs 脚本require
它?
Is it possible to install a node module, installed via npm, and then require
it from a casperjs script?
(我看到很多帖子和工具可以从 node.js 内部运行 casper 或 phantom,但这不是我想要做的.)
(I see lots of posts and tools for running casper or phantom from inside node.js, but that is not what I'm trying to do.)
casperjs 文档似乎说这是可能的,但只显示手写的玩具模块并没有真正做任何事情.我尝试安装的实际模块是 imap
,但此时我无法让任何模块工作,即使是像 net
这样的内置模块.简单例子:
The casperjs docs seem to say it is possible, but only show with hand-written toy modules that don't really do anything. The real-world module I'm trying to install is imap
, but at this point I cannot get any module to work, even built-in ones like net
. Simple example:
npm install imap
echo "var test = require('imap');" > test.js
casperjs test.js
给我:
CasperError: Can't find module imap
/usr/local/src/casperjs/bin/bootstrap.js:263 in patchedRequire
test.js:1
(我可以从 npm ls
看到 imap 模块,我可以从 node.js 脚本中很好地使用它.)
(I can see the imap module from npm ls
, and I can use it fine from a node.js script.)
或者使用内置模块:
echo "var test = require('net');" > test.js
casperjs test.js
抱怨找不到模块网络"
我有 1.4.14 的 npm --version
和 v0.10.29 的 nodejs --version
.我想知道其中任何一个都太旧了吗?(Casper 是 1.1.0-beta,Phantom 是 1.9.7,都是最新版本.)
I have npm --version
of 1.4.14 and nodejs --version
of v0.10.29. Are either of those too old, I wonder? (Casper is 1.1.0-beta, and Phantom is 1.9.7, both of which are the latest versions.)
推荐答案
PhantomJS 和 SlimerJS(用于 CasperJS 的引擎)不是 Node.js 模块.为方便起见,它们可以通过 npm 安装.它们具有与 Node.js 不同的模块基础架构.
PhantomJS and SlimerJS (the engines that are used for CasperJS) are not Node.js modules. They can be installed through npm for convenience. They have a different base infrastructure of modules which is distinct from Node.js.
您将无法使用 imap
或任何依赖于 net
模块的模块.正如 Finch 所指出的,有一些模块可以在 phantomjs 运行时内工作.
You will not be able to use imap
or any module that depends on the net
module. As Fanch points out, there are modules that can work inside the phantomjs runtime.
如果一个模块只使用了一些原生 node.js 模块的一些功能,你可以尝试更改实现以使用 phantomjs 提供的 API.不过我认为这并不容易.大多数时候你会撞到一堵墙.
If a module only uses some functionality of some native node.js module, you could try to change the implementation to use the API that phantomjs provides. I don't think this is easy though. Most of the time you will run into a wall.
在imap
的情况下,这是非常无望的.您甚至无法重新实现 require("net").Socket
,因为 phantomjs 不支持 WebSockets(至少在 1.9.7 中).
In the case of imap
, it is pretty hopeless. You cannot even re-implement the require("net").Socket
, because WebSockets are not supported in phantomjs (at least in 1.9.7).
这篇关于使用来自 casperjs 的节点模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!