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

Node.js用pkg打包screenshot-desktop后报ENOENT错误如何解决?

问题根因

pkg打包后会将所有资源打包到虚拟的C:\snapshot\文件系统中,这个路径是pkg虚拟出来的,Windows的cmd.exe无法直接访问该路径下的.bat文件,所以调用screenCapture_1.3.2.bat时会抛出ENOENT错误。直接用node运行时文件都在真实磁盘路径,所以可以正常执行。


修复步骤

  1. 修改bat文件调用逻辑
    不需要修改node_modules源码,在业务代码初始化时手动将打包内置的bat文件释放到系统真实临时目录,再覆盖screenshot-desktop的默认调用路径:
const fs = require('fs')
const path = require('path')
const os = require('os')
const screenshot = require('screenshot-desktop')

// 程序启动时先释放bat文件到系统临时目录
const tempBatPath = path.join(os.tmpdir(), 'screenCapture_1.3.2.bat')
const batContent = fs.readFileSync(path.join(__dirname, 'node_modules/screenshot-desktop/lib/win32/screenCapture_1.3.2.bat'))
if (!fs.existsSync(tempBatPath)) {
  fs.writeFileSync(tempBatPath, batContent)
}

// 覆盖screenshot-desktop的默认bat路径
screenshot.win32.screenCapturePath = tempBatPath
  1. 修正业务代码拼写错误
    现有代码里的参数名foramt为拼写错误,改为正确的format:
screenshot({format:'jpg'}).then((img) => {
    server.emit('sendshot', {
        base64: img.toString('base64')
    });
}).catch(e => {
    server.emit('Error', {message: e});
})
  1. 确认pkg配置正确
    你当前package.json里的pkg.assets配置已经包含了需要的资源文件,无需额外修改,重新打包即可生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 01:15:03