脚本在Webstorm中失败,但不是在终端上失败 [英] Script fails in Webstorm but not from terminal
问题描述
我有一个使用phantomjs-node抓取网页的nodejs脚本.当我从终端窗口运行时,它运行良好,但是当我通过Node JS应用程序的运行配置从Webstorm内部运行时,效果不佳.
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.
什么可能导致Webstorm中的错误?
What could be causing the error in Webstorm?
在注释掉.bash_profile的内容之后,我已经尝试从终端运行脚本,并且该脚本仍然有效.我还检查了另一个示例脚本中的process.env
的内容,发现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.
脚本:
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控制台输出(失败):
Webstorm console output (fails):
/usr/local/bin/node phantom.js
phantom stderr: execvp(): No such file or directory
Process finished with exit code 0
推荐答案
Webstorm确实设置了PATH变量,但它与您的应用程序在终端中运行时获得的PATH变量不同.我的解决方案是黑客:
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:
- 键入
node
进入REPL - 运行
process.env
- 复制PATH值的内容
- 向Webstorm添加一个使用此值的名为PATH的环境变量.它将覆盖Webstorm为您的应用提供的默认PATH变量.
- Type
node
to get to the REPL - Run
process.env
- Copy the contents of the PATH value
- Add an environment variable to Webstorm called PATH that uses this value. It will overwrite the default PATH variable that Webstorm gives your app.
完成!
这篇关于脚本在Webstorm中失败,但不是在终端上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!