Search code examples
node.jsterminalphantomjswebstorm

Script fails in Webstorm but not from terminal


I have a nodejs script that uses phantomjs-node to scrape a webpage. It works fine when I run from a terminal window, but not when I run from inside Webstorm via a run configuration for a Node JS application.

What could be causing the error in Webstorm?

I've already tried running the script from the terminal after commenting out the contents of .bash_profile and it still works. I've also checked the contents of process.env in another sample script and saw that the values are completely different in Webstorm vs. terminal.

The script:

var phantom = require('phantom');
phantom.create(function(ph) {
    return ph.createPage(function(page) {
        return page.open("http://www.google.com", function(status) {
            console.log("opened google? ", status);
            return page.evaluate((function() {
                return document.title;
            }), function(result) {
                console.log('Page title is ' + result);
                return ph.exit();
            });
        });
    });
});

Terminal output (works great!):

opened google?  success
Page title is Google

Webstorm console output (fails):

/usr/local/bin/node phantom.js
phantom stderr: execvp(): No such file or directory


Process finished with exit code 0

Solution

  • Webstorm does set a PATH variable, but it's different to the PATH variable your app gets when run in the terminal. My solution, a hack:

    1. Type node to get to the REPL
    2. Run process.env
    3. Copy the contents of the PATH value
    4. Add an environment variable to Webstorm called PATH that uses this value. It will overwrite the default PATH variable that Webstorm gives your app.

    Done!