screenshot-desktop

bencevans活跃维护

用 JavaScript 跨平台截取本机屏幕截图,支持 Linux、macOS 和 Windows,适合在 Node.js 脚本或自动化流程中直接调用。

💻 Capture a screenshot of your local machine

v1.15.8发布于 2026-11-02 13:30 UTC503star105fork同步于 2026-11-10 23:57 UTC

怎么上手

由 README 提炼,执行前请确认
npm install --save screenshot-desktop
硬件要求按 README 说明

README 未说明显存或 GPU 要求;Linux 需安装 ImageMagick(apt-get install imagemagick),macOS 和 Windows 无需额外依赖。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

跨平台 Node.js 截屏库,用 Promise 返回截图 Buffer 或保存为文件

用 JavaScript 跨平台截取本机屏幕截图,支持 Linux、macOS 和 Windows,适合在 Node.js 脚本或自动化流程中直接调用。

核心能力

  • 用 Promise 异步返回 JPG/PNG 等格式的截图 Buffer
  • 支持 listDisplays 枚举显示器并按 id 指定截取
  • 支持 all() 一次截取所有屏幕
  • 可传 filename 直接保存到指定路径
  • 支持 macOS 捕获鼠标光标 includeCursor

容易踩的坑

Linux 默认依赖 ImageMagick,未安装会失败;GIF 输出在 scrot 下可能失败,需改用 linuxLibrary: 'imagemagick';Windows 和 macOS 不会根据 filename 后缀推断格式,需显式设置 format;macOS 虚拟机可能返回空的显示器列表,此时需省略 screen 参数。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v1.15.8
  1. v1.15.8最新正式版2026-11-02 · 2026-11-02 13:30 UTC

    Security

    • Harden Windows capture helper extraction so each operation uses a fresh private directory and removes it after use. Windows applications should update, especially those that run screenshot capture with elevated privileges.

    Other changes since v1.15.7

    • Add TypeScript declarations and improve package compatibility with ESM and TypeScript imports.
    • Improve Windows temporary path handling, bundled helper assets, and display parsing.
    • Improve capture and cleanup behavior on macOS and Linux.

    The npm package is available as [email protected].

  2. v1.15.62026-08-13 · 2026-08-13 07:16 UTC
    查看更新说明

    Maintenance

    • Updated vulnerable transitive brace-expansion dependencies to patched versions.
    • Updated vulnerable transitive js-yaml dependencies to patched versions.
    • npm audit now reports zero vulnerabilities.

    Full changelog: https://github.com/bencevans/screenshot-desktop/compare/v1.15.5...v1.15.6

  3. v1.15.52026-08-13 · 2026-08-13 06:59 UTC
    查看更新说明

    Security

    This release fixes a Windows command-injection vulnerability in options.screen. Applications that pass untrusted input to this option could allow command metacharacters to reach cmd.exe. Requested displays are now validated against the identifiers returned by Windows before capture.

    Affected screen-injection versions are earlier than 1.15.2 and versions 1.15.3 through 1.15.4. Version 1.15.2 filtered this input but broke canonical Windows display identifiers; version 1.15.5 provides the complete fix. Users should upgrade to 1.15.5.

    Thanks to @arpitjain099 for responsibly reporting this vulnerability.

    What's Changed

    Full Changelog: https://github.com/bencevans/screenshot-desktop/compare/v1.15.4...v1.15.5

  4. v1.15.32025-10-20 · 2025-10-20 18:46 UTC
    查看更新说明

    What's Changed

    New Contributors

    Full Changelog: https://github.com/bencevans/screenshot-desktop/compare/v1.15.2...v1.15.3

  5. v1.15.12024-12-24 · 2024-12-24 11:28 UTC
    查看更新说明

    What's Changed

    New Contributors

    Full Changelog: https://github.com/bencevans/screenshot-desktop/compare/v1.14.1...v1.15.1

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    screenshot-desktop

    Capture a screenshot of your local machine

    • Multi/Cross Platform
      • Linux: required ImageMagick apt-get install imagemagick
      • OSX: No dependencies required!
      • Windows: No dependencies required!
    • Promise based API
    • JPG output (by default)

    Install

    $ npm install --save screenshot-desktop
    

    Usage

    import screenshot from 'screenshot-desktop'
    
    const img = await screenshot()
    // img: Buffer containing a JPG screenshot
    // ...
    
    const img = await screenshot({ format: 'png' })
    // img: Buffer containing a PNG screenshot
    
    const displays = await screenshot.listDisplays()
    // displays: [{ id, name }, { id, name }]
    const lastDisplay = displays[displays.length - 1]
    const img = await screenshot(lastDisplay ? { screen: lastDisplay.id } : {})
    // img: Buffer of the last display, or the default display if none are detected
    
    const imgs = await screenshot.all()
    // imgs: an array of Buffers, one for each screen
    
    const imgPath = await screenshot({ filename: 'shot.jpg' })
    // imgPath: absolute path to shot.jpg, saved in the working directory
    
    // Absolute paths work too; set format to request PNG output.
    await screenshot({ filename: '/Users/brian/Desktop/demo.png', format: 'png' })
    

    screenshot() options

    • filename Optional. Absolute or relative path to save output.
    • format Optional. See supported image formats below. Values are case-insensitive.
    • linuxLibrary Optional. Linux only. Valid values scrot|imagemagick. Which library to use. Note that scrot does not support screen selection.
    • includeCursor Optional. macOS only. Include the mouse cursor in the capture. Windows and Linux reject it when enabled; false is accepted on every platform.

    On macOS VMs, display discovery can return an empty list even when screen capture works. Omit screen to capture the default display in this case; screenshot.all() returns that capture as a one-element array. Explicit display selection still requires detected displays.

    Supported image formats

    Support depends on the platform and, on Linux, the selected capture tool. This matrix applies to both Buffer output and files saved with filename.

    formatWindowsmacOSLinux: ImageMagick (default)Linux: scrot
    jpg, jpegYesYesYesYes
    pngYesYesYesYes
    bmpYesYesYesYes
    tiffNoYesYesYes
    gifNoYesYesDepends (see below)
    pdfNoYesNoNo

    “Yes” formats were verified with native capture tools in Windows, macOS and Linux VMs, checking the actual encoding of both returned Buffers and saved files. “No” means the library rejects that format. Other values, including webp, emf, exif and wmf, are rejected on all platforms.

    Linux output also depends on the encoders available in the installed capture tool. The library accepts gif with scrot, but GIF saving failed in the tested Ubuntu 24.04 installation. Use linuxLibrary: 'imagemagick' for GIF output.

    The default output is JPEG. Set format explicitly for consistent behavior across platforms: Windows and macOS do not infer the encoding from filename. Linux infers PNG from a .png filename and otherwise defaults to JPEG; other formats still need an explicit format.

    await screenshot({ filename: 'shot.bmp', format: 'bmp' })
    

    TypeScript

    The package includes TypeScript 5+ declarations for the existing JavaScript API. Node.js types must be available in your TypeScript project (npm install --save-dev @types/node).

    import screenshot = require('screenshot-desktop')
    
    const image: Promise<Buffer> = screenshot({ format: 'png' })
    const filename: Promise<string> = screenshot({ filename: 'shot.png' })
    const options: screenshot.Options = { screen: 0 }
    

    Display IDs and metadata vary by platform. The root API exposes shared fields and optional platform-specific fields; existing platform imports such as screenshot-desktop/lib/win32 expose the corresponding display type.

    Windows bundles and Electron

    The Windows helper and manifest are embedded in the built JavaScript and extracted into a real temporary directory before capture. Node-targeted CommonJS bundles (such as esbuild output) and Electron ASAR packages carry these assets automatically; no asarUnpack or extraResources configuration is needed for them.

    The original helper files remain in the npm package for existing integrations. Call the library from Electron's main process, where Node.js APIs are available.

    Development

    Run npm ci and npm run build before loading the package from a checkout. npm test builds automatically, and npm pack builds JavaScript and declarations before packaging. Generated files are ignored by Git. Run npm run format to format TypeScript with Prettier and JavaScript with Standard; npm run lint checks formatting and types. Function documentation uses JSDoc comments alongside TypeScript signatures, so it is also available in editors and the published declarations. Native capture tests need a desktop session and the platform's screenshot tools/permissions.

    Licence

    MIT © Ben Evans