【问题标题】:Run phantomjs and casperjs on local folder在本地文件夹上运行 phantomjs 和 casperjs
【发布时间】:2016-08-27 03:15:48
【问题描述】:

我正在寻找解决一个问题的方法。我想安装和使用库 casperjs 而不在机器上全局安装任何东西。

首先我尝试通过npm install casperjs (https://www.npmjs.com/package/casperjs) 安装

显然,这是错误消息:casperjs : commande introuvable

我尝试使用静态路径:../../node_modules/casperjs/bin/casperjs scriptCasperJS.js

新错误:Fatal: [Errno 2] No such file or directory; did you install phantomjs?

所以我从website 下载并提取phantomjs 并将我的文件夹导出到环境变量:export PHANTOMJS_EXECUTABLE=/home/xxxxxxxxxx/node_modules/phantomjs-2.1.1-linux-x86_64/bin/phantomjs

没关系,但出现新错误:CasperError: Can't find module casperjs

这可能来自我的代码:

var casper = require("casperjs").create({    
    verbose: false,
    logLevel: "debug"
});

任何想法..?我必须改变用 casperjs 编码的方式吗?

提前感谢您的帮助。

编辑:为了更好地理解

- myfolder
   +- modules
   |  +- phantomjs-2.1.1-linux-x86_64
   |  | +- bin
   |  | | +- phantomjs 
   +- node_modules
   |  +- casperjs    
   |  | +- bin
   |  | | +- casperjs    
   +- scripts
   |  +- myscriptname   
   |  | +- scriptCasperJS.js   
   +- package.json  

【问题讨论】:

    标签: npm phantomjs local casperjs require


    【解决方案1】:

    编辑:看来我误解了你的问题。这是新的答案:

    要在由 casperJS 运行的脚本中包含 Casper 模块,正确的要求是

    var casper = require("casper"); // Not "casperjs" !
    

    旧答案:

    CasperJS 不是 NodeJS 模块。他们的文档中明确规定了这一点。 (请参阅该部分底部的红色警告:http://docs.casperjs.org/en/latest/installation.html#installing-from-npm)。

    提供 NPM 安装只是为了方便。您将无法执行 require,而只能将其作为外部命令调用。

    【讨论】:

    • 我知道,我的脚本是通过这个命令运行的,就像我说的:../../node_modules/casperjs/bin/casperjs scriptCasperJS.js 以前,casper 是全局安装的 (-g) 并且一切运行良好。
    • 好吧,在这种情况下,答案就更简单了。这只是一个错误的要求,我刚刚检查过。 :-)
    • 好的!所以它的工作......太棒了!但是你在哪里找到这个提示?!当然,我谢谢你,但也许“门”会等;-)
    • 它在文档中:docs.casperjs.org/en/latest/writing_modules.html ;)(另外,通常,我倾向于在 require 中添加或删除“js”部分。99% 的时间都有效。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多