我有一个 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:
- 输入
node
进入 REPL - 运行
process.env
- 复制PATH值的内容
- 向 Webstorm 添加一个名为 PATH 的环境变量,该变量使用该值。它将覆盖 Webstorm 为您的应用提供的默认 PATH 变量。
完成!
关于node.js - 脚本在 Webstorm 中失败,但不是来自终端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11175041/