[英]Puppeteer fails in React app after running electron-builder
I am using puppeteer in some back-end js within a react app.我在反应应用程序中的一些后端 js 中使用 puppeteer。
The code is fairly simple in regards to the puppeteer components and it works when testing the app in a normal development script within yarn.代码对于 puppeteer 组件来说相当简单,并且在 yarn 中的正常开发脚本中测试应用程序时它可以工作。 The likely relevant code is fairly simple:
可能相关的代码相当简单:
const puppeteer = require('puppeteer');
try {
let browser = await puppeteer.launch({headless: false,
executablePath: process.env.CHROMIUM_PATH,
args: ["--no-sandbox",
"--disable-setuid-sandbox"]
});}
catch (error) {
return await error;
}
However, when I use electron-builder
to compile the project into an executable I get the following error message:但是,当我使用
electron-builder
将项目编译成可执行文件时,出现以下错误消息:
Error: Failed to launch the browser process! spawn
<project directory in AppData>\node_modules\puppeteer\.local-chromium\win32\chrome-win\chrome.exe
ENOENT TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
I thought it might be a problem with my dependencies, but I feel like I've tried everything我认为这可能是我的依赖项的问题,但我觉得我已经尝试了一切
Anyone have any guidance?任何人有任何指导?
.local-chromium file summary: .local-chromium 文件摘要:
.local-chromium
|win32
|chrome-win
|locales
|MEIPreload
|swiftshader
|files within chrome-win
Files within chrome-win include:
-chrome.exe
-chrome_proxy.exe
-chrome_pwalauncher.exe
-chrome.dll
-Other PAK files, and smaller applications
There is nothing located in.local-chromium or win32 besides the subdirectories除了子目录外,在 .local-chromium 或 win32 中没有任何内容
As sebasaenz suggested, it was a problem with puppeteer accessing Chromium.正如 sebasaenz 所建议的,这是 puppeteer 访问 Chromium 的问题。 I looked into the directory not being zipped but all files looked extracted.
我查看了未压缩的目录,但所有文件看起来都已提取。
I couldn't figure out what the issue was with.local-chromium so I decided to switch angles and just direct puppeteer to my normal chrome.exe to run itself.我无法弄清楚.local-chromium 的问题是什么,所以我决定换个角度,只是将 puppeteer 引导到我的普通 chrome.exe 以自行运行。 You can do this with the executablePath argument in puppeteer.launch:
您可以使用 puppeteer.launch 中的 executablePath 参数来执行此操作:
let browser = await puppeteer.launch({executablePath: "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"})
chrome.exe might be in a different location for you, but I'm pretty sure mine is the default chrome.exe 可能对您来说位于不同的位置,但我很确定我的是默认位置
This did allow my app to work when packaged, unfortunately it means that it won't automatically work when moved to a different machine if the chrome path is different.这确实允许我的应用程序在打包时工作,不幸的是,这意味着如果 chrome 路径不同,它在移动到另一台机器时不会自动工作。 Not the worst trade off for a working program though.
不过,对于一个工作程序来说,这并不是最糟糕的权衡。
Puppeteer fails because it not able to find its executable, Use bellow solution this works for me Prod as well as Dev enviroment. Puppeteer失败是因为它无法找到其可执行文件,使用下面的解决方案这对我 Prod 和 Dev 环境都有效。
"build": {
"asar": true,
"asarUnpack": "node_modules/puppeteer/.local-chromium/**/*"
}
function getChromiumExecPath() {
return puppeteer.executablePath().replace('app.asar', 'app.asar.unpacked');
}
export function createBrowser(options = {}) {
return puppeteer.launch({
...options,
executablePath: getChromiumExecPath()
});
}
It seems that the issue has to do with a failed unzipping.似乎问题与解压缩失败有关。 There's actually an open issue about this on GitHub .
实际上在GitHub上有一个关于此的未解决问题。
A possible workaround is to manually unzip the zip file and extract it into the chrome-win
folder in this case.在这种情况下,一种可能的解决方法是手动解压缩 zip 文件并将其解压缩到
chrome-win
文件夹中。
For Mac (the workaround in the GitHub issue) it's something like this:对于 Mac(GitHub 问题中的解决方法)是这样的:
unzip node_modules/puppeteer/.local-chromium/chrome-mac.zip
cp -r chrome-mac/Chromium.app/ ./node_modules/puppeteer/.local-chromium/mac-756035/chrome-mac/Chromium.app/
So I think that in your case you should look for the zip file in the win32
or chrome-win
folders and copy it to the chrome-win
folder.因此,我认为在您的情况下,您应该在
win32
或chrome-win
文件夹中查找 zip 文件并将其复制到chrome-win
文件夹中。
My app installs with a resources/app.asar.unpacked directory.我的应用安装了 resources/app.asar.unpacked 目录。 The error it was throwing was
它抛出的错误是
spawn <install_directory>\win-unpacked\resources\app.asar\node_modules\puppeteer\.local-chromium\win64-856583\chrome-win\chrome.exe ENOENT
So, when starting puppeteer, I do this:因此,在启动 puppeteer 时,我会这样做:
async function openBrowser() {
var executablePath =
puppeteer
.executablePath()
.replace("app.asar", "app.asar.unpacked")
const browser = await puppeteer.launch({
executablePath: executablePath
});
return browser
}
Works on first pass, haven't distributed to users yet.首次通过,尚未分发给用户。 Haven't tested on OSX.
没有在 OSX 上测试过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.