javascriptnode.jscasperjsspookyjs

Using CasperJS within a regular JavaScript file


I am trying to create a NodeJS program that utilizes CasperJS within it. I have run into the error that the module 'casper' cannot be found. As a result, I have tried to npm install spooky --save as I have read around that it is a driver, but I am still getting the same error as I was getting before.

Also, before trying to install SpookyJS, I tried

 var phantom = require('phantom');

 phantom.casperPath = '/path/to/casperjs';
 phantom.injectJs(phantom.casperPath + '/bin/bootstrap.js');

Which then gave me the error that injectJs is not a function. Any and all help appreciated.


Solution

  • First of all, to contextualize a bit, here is an important reminder for everyone:

    While CasperJS is installable via npm, it is not a NodeJS module and will not work with NodeJS out of the box. You cannot load casper by using require(‘casperjs’) in node. Note that CasperJS is not capable of using a vast majority of NodeJS modules out there. Experiment and use your best judgement.

    This is where SpookyJS comes into play... But how to make it work? (I assume you are on Linux.)

    1. Make sure you have the right environment

    Note: SpookyJS works on my computer (Arch Linux) and I have the following setup:

    node --version ---> v7.7.4
    npm --version ---> 4.4.4
    phantomjs --version ---> 2.1.1
    casperjs --version ---> 1.1.3

    PhantomJS and CasperJS are installed globally.

    2. Install SpookyJS locally (and its dependency: tiny-jsonrpc)

    Create an empty directory and run npm i spooky tiny-jsonrpc inside. We do not need a package.json here, so you can forget about --save or --save-dev.

    3. Test the given example

    If SpookyJS is installed, you should have a local node_modules directory. Now, try to run the following command:

    node node_modules/spooky/examples/hello.js
    

    If you get "Hello, from Spooky the Tuff Little Ghost - Wikipedia", congrats! You can now integrate SpookyJS in your project, but you will have to respect the syntax presented in hello.js...