title: How to Use CasperJS in node.js?
body: <p>I would like to use CasperJS in node.js.</p>
  
  <p>I have referred to the following URL's to use CasperJS in node.js:</p>
  
  <ul>
  <li><a href="https://github.com/sgentle/phantomjs-node" rel="noreferrer">https://github.com/sgentle/phantomjs-node</a></li>
  <li><a href="http://casperjs.org/index.html#faq-executable" rel="noreferrer">http://casperjs.org/index.html#faq-executable</a></li>
  </ul>
  
  <p>With the help of the above URLs I have written the following code:</p>
  
  <pre><code>//DISPLAY=:0 node test2.js
  var phantom = require('phantom');
  console.log('Hello, world!');
  phantom.create(function (ph) {
      ph.casperPath = '/opt/libs/casperjs'
      ph.injectJs('/opt/libs/casperjs/bin/bootstrap.js');
      var casper = require('casper').create();
      casper.start('http://google.fr/');
  
      casper.thenEvaluate(function (term) {
          document.querySelector('input[name="q"]').setAttribute('value', term);
          document.querySelector('form[name="f"]').submit();
      }, {
          term: 'CasperJS'
      });
  
      casper.then(function () {
          // Click on 1st result link
          this.click('h3.r a');
      });
  
      casper.then(function () {
          console.log('clicked ok, new location is ' + this.getCurrentUrl());
      });
  
      casper.run();
  });
  </code></pre>
  
  <hr>
  
  <p>When I run this code, I got the following error:</p>
  
  <p><em>ERROR MSG:</em></p>
  
  <pre><code>tz@tz-ubuntu:/opt/workspaces/TestPhantomjs$ DISPLAY=:0 node test2.js 
  Hello, world!
  Error: Cannot find module 'casper'
      at Function._resolveFilename (module.js:332:11)
      at Function._load (module.js:279:25)
      at Module.require (module.js:354:17)
      at require (module.js:370:17)
      at /opt/workspaces/TestPhantomjs/test2.js:6:14
      at Object.&lt;anonymous&gt; (/opt/workspaces/TestPhantomjs/node_modules/phantom/phantom.js:82:43)
      at EventEmitter.&lt;anonymous&gt; (/opt/workspaces/TestPhantomjs/node_modules/phantom/node_modules/dnode/index.js:215:30)
      at EventEmitter.emit (events.js:67:17)
      at handleMethods (/opt/workspaces/TestPhantomjs/node_modules/phantom/node_modules/dnode-protocol/index.js:138:14)
      at EventEmitter.handle (/opt/workspaces/TestPhantomjs/node_modules/phantom/node_modules/dnode-protocol/index.js:98:13)
  phantom stdout: Unable to load casper environment: Error: Failed to resolve module fs, tried fs
  </code></pre>