Search code examples
node.jsherokuphantomjscasperjs

How to run Node controlled Phantomjs/Casperjs script in Heroku


I've written a Casperjs script to do some scraping, however, after running into memory exhaustion issues, I've now written a node script to turn Phantom off and on via exec. I can run this with no issues locally, however when I deploy to heroku I get the following error

Error: Command failed: casperjs turnitoffandon.js
ERROR: stderr was:/bin/sh 1: casperjs: not found

I used the nodejs buildpack and have Phantom and Casper defined in my dependencies. In the heroku bash, running phantomjs --version returns 2.1.1 and casperjs --version returns 1.1.4.

Do I need to define where Casper is? If so how? I have set my PATH variable as /usr/local/bin:/usr/bin:/bin:/app/vendor/phantomjs/bin:/app/vendor/casperjs/bin:/node_module/casperjs/bin , like in this SO question


Solution

  • The issue actually had nothing to do with Heroku. As noted in this SO answer, using exec and providing any environmental variables in the options parameter replaces the entire set of environment variables. This includes the path, effectively overwriting any paths already specified to Heroku in the buildpack and npm modules.

    You can create a copy of process.env and pass that along in the parameters in addition to other environmental parameters needed to be passed.