25

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 ?

AsTeR
  • 7,247
  • 14
  • 60
  • 99

1 Answers1

58

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);
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
hexid
  • 3,811
  • 1
  • 30
  • 40
  • 9
    Found this answer after hours of frustration - Doh! Anyone know why they use separate node modules or couldn't have consistent naming for the few functions they have? It seems very very counter-intuitive and has caused other developers much frustration as well. – Zaheer Dec 30 '13 at 05:42
  • @Zaheer Casperjs is a Qt implementation, not a nodejs library or similar, so they had to reinvent the wheel – Purefan Sep 20 '17 at 11:16