【问题标题】:Use a node module from casperjs使用来自 casperjs 的节点模块
【发布时间】:2014-08-14 18:54:23
【问题描述】:

是否可以安装一个节点模块,通过 npm 安装,然后通过 casperjs 脚本 require 它?

(我看到很多从 node.js 中运行 casper 或 phantom 的帖子和工具,但这不是我想要做的。)

casperjs 文档似乎说这是可能的,但只显示了手写的玩具模块,它们并没有真正做任何事情。我要安装的真实模块是imap,但此时我无法让任何模块工作,即使是像net 这样的内置模块。简单例子:

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 脚本中很好地使用它。)

或者使用内置模块:

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,两者都是最新版本。)

【问题讨论】:

    标签: node.js npm phantomjs casperjs


    【解决方案1】:

    PhantomJS 和 SlimerJS(用于 CasperJS 的引擎)不是 Node.js 模块。为方便起见,它们可以通过 npm 安装。它们具有与 Node.js 不同的模块基础架构。

    您将无法使用imap 或任何依赖于net 模块的模块。正如 Fanch 所指出的,有些模块可以在 phantomjs 运行时中运行。

    如果一个模块只使用了一些原生 node.js 模块的某些功能,你可以尝试更改实现以使用 phantomjs 提供的 API。我不认为这很容易。大多数时候你会撞到墙。

    imap 的情况下,它是相当绝望的。您甚至无法重新实现 require("net").Socket,因为 phantomjs 不支持 WebSockets(至少在 1.9.7 中)。

    【讨论】:

    • 这个解释真的应该加在文档里。 和 PhantomJS 一样,CasperJS 允许使用通过 npm 安装的 nodejs 模块。 -> 这句话很混乱
    • @Fanch 我创建了一个 PR 以使文档更清晰:github.com/n1k0/casperjs/pull/1406
    【解决方案2】:

    这里是颜色模块的示例:

    var colors = require('colors');
    
    
     casper.test.begin('\n*Colors module*\n', function suite(test) {
        casper.start()
        .thenOpen('https://www.google.fr/', function() {
            console.log("test require\n".zebra);
            console.log("test require\n".rainbow);
            console.log("test require\n".red.underline.bold);
          })
        .run(function() {
                test.done();
        });
    });
    
    • 节点模块
      • 颜色
    • testnode.js

    casperjs test testnode.js

    输出:

    当需要的模块有依赖时,似乎没有那么简单。

    【讨论】:

    • 谢谢!你的例子对我来说失败了,(我做了npm install colors 然后使用了你的确切脚本)所以至少这告诉我我的机器上有问题。
    • 通过“不那么简单”,很高兴知道它是不可能的还是可行的。
    【解决方案3】:

    就我而言,我想加载 underscorejs。 Underscore 是一系列函数,与 javascript 对象没有复杂的交互,所以只需要 javascript 文件,然后访问它的函数是没有问题的。

    我首先从找到我的 nodejs 安装的根目录(从 CLI):

    node --help
    

    这让我找到了我的节点路径:

    echo $NODE_PATH
    

    当时在:

    /usr/local/lib/node_modules/
    

    下划线在

    /usr/local/lib/node_modules/underscore/underscore.js
    

    所以我在 CasperJS 脚本中的最后一个 require 语句是。

    var _ = require('/usr/local/lib/node_modules/underscore/underscore.js');
    

    现在在我的脚本中测试下划线js是否已加载:

    this.echo(_.now());
    

    我看到了当前时间。

    CAVEAT:由于这是异步运行的,如果你把你的 _.now() 语句放在 require 之后,它会给你一个未定义的对象错误。作为说明,我使用的是 Casper 1.1,它使用 PhantomJS 的原生 require() 函数。如果您使用的是

    更新: 既然是这种情况,我使用 CasperJS then() 函数同步加载我的实用程序,确保在全局范围内声明我的变量。看起来是这样的:

    //at the top of the file-ish, declare variables that will hold loaded libraries.
    var utils, _;
    var casper = require('casper').create(); //create casper
    casper.start('http://example.com'); //start casper at URL.
    
    casper.then(function loadRequires(){ //load the requirements
        utils = require('utils', function(){this.echo('utils loded')});
        _ = require('/usr/local/lib/node_modules/underscore/underscore.js');
    });
    
    casper.then(function myAwesomeStuff() {
        this.echo(_.now()); //now, access the loaded requirements
        utils.dump('this stuff is soooo awesome!!!!!!!!!!!!!!!!!!!!');
        //do stuff on the page you opened in the start function here.
        );
    });
    

    您可以在 API 文档中阅读有关 Casper 原型和 then() 方法的更多信息:http://casperjs.readthedocs.org/en/latest/modules/casper.html#casper-prototype

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      相关资源
      最近更新 更多