使用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
模块的任何模块.正如Fanch所指出的,有一些模块可以在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(至少在1.9.7中)不支持WebSocket.
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屋!