Windows上来自Node的PhantomJS [英] PhantomJS from Node on Windows

查看:162
本文介绍了Windows上来自Node的PhantomJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Node,电子样板幻影.在我的linux机器上,它对我来说工作得很好,我将源代码复制到Windows 10,并使用npm start运行,一切顺利.

I have written a Electron application using Node, Electron Boilerplate, and phantom. It works perfectly fine for me on my linux machine, I copied the source over to Windows 10, and ran with npm start, and all goes smoothly.

但是,当我尝试使用npm run release使用样板模块构建应用程序时,事情进展得不太顺利.我可以很好地安装和打开该应用程序,但是当我单击激活幻象模块的按钮时,窗户变成白色,什么也没发生.我能够使用开发工具记录一些错误.

However, when I try to build the application with the boilerplate module using npm run release, things go a little less smoothly. I can install and open the application just fine, but when I click the button that activates the phantom module, the windows goes all white and nothing happens. I was able to logs some errors with the dev tools.

首先,我有:

C:\...\dist\win-unpacked\resources\app.asar\node_modules\phantom\lib\phantom.js:361
Uncaught (in promise) Error: Error reading from stdin: Error: write EPIPE(…)

我对类似问题进行了一些研究,即此处,在我看来,问题在于使用npm模块幻影启动了子进程PhantomJS.最初,我使用的是我用C#编写的WPF应用程序来启动该过程,并且效果很好.这使我相信幻像模块是罪魁祸首.

I did some research into similar issues, namely here, and it seems to me the issue is starting the child process, PhantomJS, with the npm module phantom. Originally, I was using a WPF application I wrote in C# to start the process, and that worked just fine. This leads me to believe that the phantom module is the culprit.

因此,我尝试将npm幻像模块换成 horseman ,但是得到了类似的结果结果:

So I tried swapping out the npm phantom module for horseman, but got similar results:

Unhandled rejection HeadlessError: Phantom immediately exited with: 4294967295
    at ChildProcess.immediateExit (C:\...\dist\win-unpacked\resources\app.asar\node_modules\node-horseman\node_modules\node-phantom-simple\node-phantom-simple.js:153:23)
    at ChildProcess.g (events.js:286:16)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:204:12)

推荐答案

这里是黑暗中的一枪.我不太肯定这可以解决您的问题,但是可以解决了:

Here is a shot in the dark. I am not positive this will solve your issue but here it goes:

GYP和未匹配的二进制文件

Phantom和许多其他节点模块使用为将要运行的特定操作系统而构建的二进制文件.有时,在npm日志文件中,您会看到对node-gyp的引用. Node-gyp只是有助于在节点模块中构建本机附加组件.构建二进制文件时,通常会针对三个主要参数(操作系统,cpu体系结构和正在执行安装的节点的版本)来构建二进制文件.

Phantom and many other node modules use binaries built for the specific OS that it will be running on. Sometimes in your npm log files you will see references to node-gyp. Node-gyp simply helps to build native add-on's in node modules. When the binaries are built they are usually built against, among others, three main parameters, the operating system, cpu architecture and version of node that is doing the installation.

我认为您需要将phantomjs重建为Electron正在使用的节点版本.大多数情况下,您在计算机上安装的节点版本与在Electron中运行的节点版本是不同的. Electron会尽一切努力保持最新状态,但是由于保持最新状态需要进行大量的工作和测试,因此总会有一些滞后.

I think you need to rebuild phantomjs to the version of node Electron is using. Most of the time the node version you have installed on your machine and the node version running in Electron are not the same. Electron does its best to keep up, but there is always a little lag because of the amount of work and testing required to keep up-to-date.

通过运行npm install phantom安装幻像时,它将假定它需要安装或构建您的计算机所使用的节点版本的二进制文件.然后,当您的Electron应用尝试运行幻像时,它将尝试调用 Electron的节点版本的二进制文件.如果不存在,子进程将立即退出并显示错误.

When you install phantom by running npm install phantom it will assume it needs to install or build the binaries for the node version your machine is using. Then when your Electron app tries to run phantom it tries to call the binary of the Electron's node version. When it isn't there the child process immediately exits with an error.

如何修复

幸运的是,还有其他人已经找到了解决此问题的方法,并创建了一个很好的工具来帮助生成正确的二进制文件.

Luckily, there are other people out there that have figured out how to fix this issue and have created a great tool to help generate the correct binaries.

输入electron-rebuild:

https://github.com/electron/electron-rebuild

Electron-rebuild可以在命令行中运行,它将所有您的本机模块重建为您的项目正在使用的Electron版本.

Electron-rebuild can be run in the command line, and it will rebuild all of your native modules to the version of Electron your project is using.

要安装:

npm install electron-prebuilt

要使用(在Windows中):

To use (in Windows):

.\node_modules\.bin\electron-rebuild.cmd

这足以将正确的二进制文件放在正确的位置.

This should be enough to put the correct binaries in the right place.

其他想法

有时,您可以使用使用名为node-pre-gyp的依赖项的程序包.例如. sqlite3.尝试为Electron重建程序包时,我遇到了一个已知问题.基本上,为了避免出现此问题(如果遇到此问题),只需将--pre-gyp-fix附加到上面的命令中即可.

Sometimes you can use a package that uses a dependency called node-pre-gyp. E.g. sqlite3. There is a known issue I ended up running into when trying to rebuild my packages for Electron. Basically, in order to avoid this issue (if you run into it) just append --pre-gyp-fix to the above command.

针对遇到石膏问题修复前问题的人

pre-gyp-fix上的另一件事:如果您的一个或多个依赖项依赖于需要pre-gyp-fix的模块之一,那么即使它们依赖错误的位置,它们也会在错误的位置查找二进制文件在Electron中运行.所有预gyp二进制文件都存储在类似于以下的文件夹中:

One more thing on the pre-gyp-fix: If one or more of your dependencies depends on one of the modules that need the pre-gyp-fix then they will be looking for the binary in the wrong place even if they are running in Electron. All of the pre-gyp binaries are stored in a folder similar to this:

.\node_modules\sqlite3\lib\binding

在我当前的项目中,我在这里有三个文件夹,一个用于Electron-v1.4,两个用于node-v46和node-v50. (黑客警报)为了使sqlite3与我的其他依赖项一起工作,我复制了Electron-v1.4文件夹中找到的二进制文件并将其放在两个node-v *文件夹中.这样,在Electron中运行时,即使所有依赖项都在错误的位置寻找它们,它们也都在运行正确的二进制文件. (结束黑客警报)

In my current project I have three folders here, one for Electron-v1.4, and two for node-v46 and node-v50. (hack alert) In order to have sqlite3 work with my other dependencies I copy the binary found in the Electron-v1.4 folder and put it in both node-v* folders. That way when running in Electron, all dependencies are running the correct binaries even though they are looking for them in the wrong place. (end hack alert)

结论

我无法确定这与您所看到的问题有关.但是值得一试,看看它是否可以解决您的问题.如果不是这样的话,至少我希望我可以帮助遇到同样问题的其他人.

There is no way I can be sure this has anything to do with the issue you are seeing. But it is worth a shot to see if it fixes your problem. If not then at least I hope I can help someone else experiencing the same issues I ran into.

这篇关于Windows上来自Node的PhantomJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆