Casperjs requiring local JSON file

568 Views Asked by At

I am trying to require local JSON files (such as config files) and pass those JSON objects to evaluate. For each config file, evaluate function will return different results depending on the given CSS selectors from config JSON.

For example: The folder structure is like this:

rootdir
  casperExample.js
  config/
    |_example.json

example.json

{
    "title": "$('div.pointslocal-details-section h1').text();",
    "date": "$('div.pointslocal-details-time p').text();"
}

casperExample.js

var casper = require('casper').create();
var require = patchRequire(require);
var json = require('./config/example.json');

casper.start('https://website/to/be/scraped');

casper.then(function(){
    this.echo(json);
    pageData = this.evaluate(function(json){
        var results = {};
        results['title'] = json.title;
        results['date'] = json.date;
        return results;
    }, json);
    this.echo(pageData);
});

casper.run(function(){
    this.exit();
});

This is what I get when I try to run: casperjs casperExample.js

CasperError: Can't find module ./config/example.json
  C:/Users/msarc/coding/casper/rootdir/phantomjs:/code/bootstrap.js:307 in patchedRequire

and if I use var json = require('./config/example'); (without .json) I get

SyntaxError: Expected token '}'
C:/Users/msarc/coding/casper/rootdir/phantomjs:/platform/example.js:32 in loadModule
C:/Users/msarc/coding/casper/rootdir/phantomjs:/platform/bootstrap.js:282 in _compile
C:/Users/msarc/coding/casper/rootdir/phantomjs:/platform/bootstrap.js:126 in .js
C:/Users/msarc/coding/casper/rootdir/phantomjs:/platform/bootstrap.js:278 in _load
C:/Users/msarc/coding/casper/rootdir/phantomjs:/platform/bootstrap.js:311 in require
C:/Users/msarc/coding/casper/rootdir/phantomjs:/platform/bootstrap.js:263 in require
C:/Users/msarc/coding/casper/rootdir/phantomjs:/code/bootstrap.js:302 in patchedRequire

I want to eventually make multiple config files, each with different selectors for different websites. casperjs version: 1.1.4 phantomjs version: 2.1.1

1

There are 1 best solutions below

2
On BEST ANSWER

You're requireing json file as if it were a javascript module, which it is of course not, hence the error. Instead you need to read the file and process it is JSON structure:

var fs = require('fs');
var fileContents = fs.read('config/_example.json');
var json = JSON.parse(fileContents);

Then proceed working as planned.