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

时间:2021-11-22 05:27:41

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.

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

What could be causing the error in Webstorm?

什么可能导致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.

在注释掉.bash_profile的内容后,我已经尝试从终端运行该脚本,它仍然有效。我也检查了流程的内容。在另一个示例脚本中,env发现Webstorm和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):

Webstorm控制台输出(失败):

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


Process finished with exit code 0

2 个解决方案

#1


25  

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:

Webstorm确实设置了路径变量,但是它与你的应用在终端上运行时得到的路径变量不同。我的解决方案,一个黑客:

  1. Type node to get to the REPL
  2. 输入节点以到达REPL
  3. Run process.env
  4. 运行process.env
  5. Copy the contents of the PATH value
  6. 复制路径值的内容
  7. Add an environment variable to Webstorm called PATH that uses this value. It will overwrite the default PATH variable that Webstorm gives your app.
  8. 向Webstorm添加一个名为PATH的环境变量来使用这个值。它将覆盖Webstorm给你的应用程序的默认路径变量。

Done!

完成了!

#2


1  

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.

如果你在Mac上,请查看http://devnet.jetbrains.net/docs/DOC-1160。这个文档最初是为RubyMine编写的,但是它也适用于WebStorm。

#1


25  

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:

Webstorm确实设置了路径变量,但是它与你的应用在终端上运行时得到的路径变量不同。我的解决方案,一个黑客:

  1. Type node to get to the REPL
  2. 输入节点以到达REPL
  3. Run process.env
  4. 运行process.env
  5. Copy the contents of the PATH value
  6. 复制路径值的内容
  7. Add an environment variable to Webstorm called PATH that uses this value. It will overwrite the default PATH variable that Webstorm gives your app.
  8. 向Webstorm添加一个名为PATH的环境变量来使用这个值。它将覆盖Webstorm给你的应用程序的默认路径变量。

Done!

完成了!

#2


1  

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.

如果你在Mac上,请查看http://devnet.jetbrains.net/docs/DOC-1160。这个文档最初是为RubyMine编写的,但是它也适用于WebStorm。