Script fails in Webstorm but not from terminal

2020-02-25 08:19发布

问题:

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

回答1:

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!



回答2:

If you are on Mac see http://devnet.jetbrains.net/docs/DOC-1160. This document was originally written for RubyMine, but it is applicable for WebStorm too.