You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Electron+Puppeteer打包后跨机器运行找不到Chrome的问题求助

问题:Electron + Puppeteer 打包后跨机器运行找不到Chrome

我用Electron开发桌面应用,通过Puppeteer生成PDF。本地npm运行、electron-forge package打包后本地运行都正常,但把打包文件复制到其他机器,或用electron-forge make制作安装包安装后,出现以下报错:

Error: Could not find Chrome (ver. 124.0.6367.91). This can occur if either

  1. you did not perform an installation before running the script (e.g. npx puppeteer browsers install chrome) or
  2. your cache path is incorrectly configured (which is: %USERPROFILE%.cache\puppeteer).
    For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.
    at ChromeLauncher.resolveExecutablePath (\sahacharya_fii_billgenerator-win32-x64\resources\app.asar\node_modules\puppeteer-core\lib\cjs\puppeteer\node\ProductLauncher.js:295:27)
    at ChromeLauncher.executablePath (\sahacharya_fii_billgenerator-win32-x64\resources\app.asar\node_modules\puppeteer-core\lib\cjs\puppeteer\node\ChromeLauncher.js:209:25)
    at ChromeLauncher.computeLaunchArguments (\sahacharya_fii_billgenerator-win32-x64\resources\app.asar\node_modules\puppeteer-core\lib\cjs\puppeteer\node\ChromeLauncher.js:89:37)
    at async ChromeLauncher.launch (\sahacharya_fii_billgenerator-win32-x64\resources\app.asar\node_modules\puppeteer-core\lib\cjs\puppeteer\node\ProductLauncher.js:70:28)
    at async generatePDF (\sahacharya_fii_billgenerator-win32-x64\resources\app.asar\main.js:560:25)
    at async \sahacharya_fii_billgenerator-win32-x64\resources\app.asar\main.js:508:25)
    at async WebContents. (node:electron/js2c/browser_init:2:82793)

我尝试过以下方法,但仅在本地打包后有效,其他机器仍报相同错误:

  • 设置Chrome可执行路径:
    const browser = await puppeteer.launch({ executablePath: myPath });
    
    明明传入的是字符串,却收到“path should be in string format”报错
  • 在Puppeteer目录下创建.local-chromium文件夹并复制Chrome进去
  • 在forge.config.js中指定Chromium版本

解决方案

1. 正确打包Chromium到应用目录

核心问题是打包时未将Puppeteer依赖的Chromium纳入应用包,其他机器无对应版本的Chrome/Chromium,且无法自动下载。

操作步骤:

  • 替换puppeteer为puppeteer-core(避免默认将Chrome下载到用户目录):
    npm uninstall puppeteer
    npm install puppeteer-core
    
  • 手动下载与报错版本匹配的Chromium(124.0.6367.91),放到项目内的固定目录,比如./assets/chromium
  • 在代码中通过Electron提供的路径API获取正确的Chromium可执行文件路径:
    const { app } = require('electron');
    const path = require('path');
    const puppeteer = require('puppeteer-core');
    
    // 区分开发/生产环境路径
    const chromiumPath = process.env.NODE_ENV === 'development' 
      ? path.join(__dirname, './assets/chromium/chrome.exe') // 开发环境
      : path.join(process.resourcesPath, './assets/chromium/chrome.exe'); // 打包后环境
    
    async function generatePDF() {
      const browser = await puppeteer.launch({
        executablePath: chromiumPath,
        headless: 'new' // 使用新版无头模式
      });
      // 后续PDF生成逻辑
    }
    

2. 配置Electron Forge打包规则

修改forge.config.js,确保打包时包含Chromium目录:

module.exports = {
  packagerConfig: {
    // 将chromium目录加入打包资源
    extraResource: ['./assets/chromium'],
  },
  // 其他原有配置...
};

打包后,Chromium会被复制到应用的resources/assets/chromium目录,与代码中的路径对应。

3. 解决“path should be in string format”报错

该错误通常是因为打包后__dirname在app.asar中的解析行为异常,使用process.resourcesPath(Electron提供的资源目录绝对路径)可避免此问题。也可通过path.resolve确保路径格式正确:

const chromiumPath = path.resolve(process.resourcesPath, './assets/chromium/chrome.exe');

4. 验证打包结构

打包完成后,检查应用目录:sahacharya_fii_billgenerator-win32-x64/resources/assets/chromium,确认Chrome可执行文件存在且路径正确。


内容的提问来源于stack exchange,提问作者kaushik balamurali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 04:07:40