脚本在Webstorm中失败,但在终端中失败

时间:2012-06-24 04:53:10

标签: node.js terminal phantomjs webstorm

我有一个nodejs脚本,它使用phantomjs-node来抓取网页。当我从终端窗口运行时,它工作正常,但是当我通过Node JS应用程序的运行配置从Webstorm内部运行时,它没有。

什么可能导致Webstorm中的错误?

我已经尝试在注释掉.bash_profile的内容后从终端运行脚本,但它仍然有效。我还检查了另一个示例脚本中process.env的内容,发现Webstorm与终端的值完全不同。

剧本:

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();
            });
        });
    });
});

终端输出(效果很好!):

opened google?  success
Page title is Google

Webstorm控制台输出(失败):

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


Process finished with exit code 0

2 个答案:

答案 0 :(得分:25)

Webstorm确实设置了一个PATH变量,但它与你的app在终端中运行时得到的PATH变量不同。我的解决方案,一个黑客:

  1. 输入node以转到REPL
  2. 运行process.env
  3. 复制PATH值的内容
  4. 向Webstorm添加一个名为PATH的环境变量,该变量使用此值。它将覆盖Webstorm为您的应用程序提供的默认PATH变量。
  5. 完成!

答案 1 :(得分:1)

如果您使用的是Mac,请参阅http://devnet.jetbrains.net/docs/DOC-1160。本文档最初是为RubyMine编写的,但也适用于WebStorm。