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

在Deno中使用node-html-to-image遇Chrome未找到错误如何解决?

在Deno环境中正常使用node-html-to-image的解决方案

问题原因

node-html-to-image依赖puppeteer-cluster,而Puppeteer在Deno环境下不会自动下载Chromium,且Deno默认阻止启动外部进程,导致出现浏览器未找到的错误。

解决步骤

1. 补充Deno必要权限

运行脚本时,除了你已经添加的--allow-env、--allow-read、--allow-write,还需要添加--allow-run(允许启动浏览器进程)和--allow-net(若需下载浏览器或浏览器联网):

deno run --allow-env --allow-read --allow-write --allow-run --allow-net that-file.ts

2. 指定本地已安装的Chrome/Chromium路径

如果你本地已经安装了Chrome或Chromium,可以在代码中直接指定其可执行文件路径,让node-html-to-image调用现有浏览器:

修改你的TypeScript代码:

import nodeHtmlToImage from "npm:node-html-to-image";

nodeHtmlToImage({
  output: './image.png',
  html: '<html><body>Hello world!</body></html>',
  // 配置Puppeteer使用本地浏览器
  puppeteer: {
    launch: {
      // 根据你的系统替换路径
      // macOS Chrome路径
      executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
      // macOS Chromium路径示例:'/Applications/Chromium.app/Contents/MacOS/Chromium'
      // Windows Chrome路径示例:'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
      // Linux Chrome路径示例:'/usr/bin/google-chrome'
      // Linux Chromium路径示例:'/usr/bin/chromium-browser'
    }
  }
})
.then(() => console.log('The image was created successfully!'));

3. 手动下载指定版本的Chromium

如果本地没有合适的浏览器版本,可手动下载报错提示的Chromium 982053版本:

运行以下命令下载对应版本的浏览器:

deno run -A npm:puppeteer browsers install chrome@982053

下载完成后,找到Deno缓存中的Chromium路径(例如macOS下路径可能为~/Library/Caches/deno/npm/registry.npmjs.org/puppeteer/<版本号>/chrome/mac_arm64/chrome-mac/Chromium.app/Contents/MacOS/Chromium,根据你的系统架构调整),将其填入代码的executablePath中即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 05:20:42