node.js - 脚本在 Webstorm 中失败,但不是来自终端

标签 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

最佳答案

Webstorm 确实设置了一个 PATH 变量,但它与您的应用在终端中运行时获得的 PATH 变量不同。我的解决方案,一个 hack:

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

完成!

关于node.js - 脚本在 Webstorm 中失败,但不是来自终端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11175041/

相关文章:

python - 如何从 Python 脚本在终端中执行命令?

javascript - CasperJS - 下载没有 URL 的生成文件

java - selenium + phantomJS Web 元素内部异常

google-maps - 启用我的 Node.js Web 应用程序来保存谷歌地图图片

c++ - 如何在C++文件中声明标识符JSON?

node.js - 如何生成上传到 firebase 存储的文件的访问 token ?

ubuntu - 如何将默认终端设置为终结者?

javascript - phantomjs 不评估 javascript 表

java - 在虚拟机上运行 selenium grid 的问题

javascript - 如何使用 Node.js 标记化 Markdown ?