Yuta Watanabe's Blog

開発周りと、その他諸々

CasperJSとPhantomJSをnpmからインストールする

f:id:yuwata:20170207130857p:plain

WebkitベースのHeadlessブラウザ PhantomJS と、そのPhantomJSを簡単にあるいは便利に使えるようにしたユーティリティライブラリ CasperJSを、npm経由でインストールしました。PATHを通す等、少しハマったので備忘録です。

    $npm install -g phantomjs
    $npm install -g casperjs

ここで $casperjs を呼び出すと、以下のようなエラーが出ました。

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

 Google で色々検索するとPATHが通っていなくCasperJSがPhantomJSを見つけられないとのこと。CasperJSを試してみる - Qiita を参考に、以下を .bashrc に追記。

    # PhantomJS with CasperJS from npm
    phantomjs=$HOME/.nodebrew/current/bin/phantomjs
    [ -d "$phantomjs" ] &&
      export PATH=$phantomjs:$PATH
    [ ! -x "$phantomjs/phantomjs" ] &&
      chmod +x "$phantomjs/phantomjs"

ターミナルを再起動して、再度 CasperJSを実行。バージョンが問題なく表示されることを確認。

    $casperjs --version
    1.1.3