File IO inside CasperJS

AsTeR picture AsTeR · Mar 7, 2013 · Viewed 12.1k times · Source

Is it possible do read/write files inside a CasperJS Script ?

var fs = require('fs');
var data = fs.readFileSync('testdata.data', 'utf-8');
console.log(data);

Calling casperjs fileio.jsreturns:

'undefined' is not a function

Even after running npm install fs.

Bonus point if not : explain why ?

Answer

hexid picture hexid · Mar 8, 2013

CasperJS does not make use of NodeJS' fs module. Instead, it uses that of PhantomJS

Here's a short example of how to use PhantomJS' filesystem module:

var fs = require('fs');
var utils = require('utils');
var data = fs.read('testdata.dat');
utils.dump(data);