Linux 默认依赖 ImageMagick,未安装会失败;GIF 输出在 scrot 下可能失败,需改用 linuxLibrary: 'imagemagick';Windows 和 macOS 不会根据 filename 后缀推断格式,需显式设置 format;macOS 虚拟机可能返回空的显示器列表,此时需省略 screen 参数。
npm install --save screenshot-desktopREADME 未说明显存或 GPU 要求;Linux 需安装 ImageMagick(apt-get install imagemagick),macOS 和 Windows 无需额外依赖。
跨平台 Node.js 截屏库,用 Promise 返回截图 Buffer 或保存为文件
用 JavaScript 跨平台截取本机屏幕截图,支持 Linux、macOS 和 Windows,适合在 Node.js 脚本或自动化流程中直接调用。
核心能力
- 用 Promise 异步返回 JPG/PNG 等格式的截图 Buffer
- 支持 listDisplays 枚举显示器并按 id 指定截取
- 支持 all() 一次截取所有屏幕
- 可传 filename 直接保存到指定路径
- 支持 macOS 捕获鼠标光标 includeCursor
容易踩的坑
- 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]. - v1.15.62026-08-13 · 2026-08-13 07:16 UTC
查看更新说明
Maintenance
- Updated vulnerable transitive
brace-expansiondependencies to patched versions. - Updated vulnerable transitive
js-yamldependencies to patched versions. npm auditnow reports zero vulnerabilities.
Full changelog: https://github.com/bencevans/screenshot-desktop/compare/v1.15.5...v1.15.6
- Updated vulnerable transitive
- 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 reachcmd.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
- chore(deps-dev): bump tar from 7.5.11 to 7.5.16 by @dependabot[bot] in https://github.com/bencevans/screenshot-desktop/pull/342
- chore(deps-dev): bump tar from 7.5.16 to 7.5.22 by @dependabot[bot] in https://github.com/bencevans/screenshot-desktop/pull/343
- fix(win32): validate requested display in https://github.com/bencevans/screenshot-desktop/pull/344
- fix(win32): avoid temp directory race in https://github.com/bencevans/screenshot-desktop/pull/345
Full Changelog: https://github.com/bencevans/screenshot-desktop/compare/v1.15.4...v1.15.5
- v1.15.32025-10-20 · 2025-10-20 18:46 UTC
查看更新说明
What's Changed
- chore(deps): bump actions/setup-node from 4 to 5 by @dependabot[bot] in https://github.com/bencevans/screenshot-desktop/pull/316
- Fixed display selection by @shyMartinVR in https://github.com/bencevans/screenshot-desktop/pull/317
New Contributors
- @shyMartinVR made their first contribution in https://github.com/bencevans/screenshot-desktop/pull/317
Full Changelog: https://github.com/bencevans/screenshot-desktop/compare/v1.15.2...v1.15.3
- v1.15.12024-12-24 · 2024-12-24 11:28 UTC
查看更新说明
What's Changed
- Remove Pinkie Promise by @bencevans in https://github.com/bencevans/screenshot-desktop/pull/282
- chore(deps): update dependencies by @bencevans in https://github.com/bencevans/screenshot-desktop/pull/283
- chore(ci): switch to coactions/setup-xvfb@v1 by @bencevans in https://github.com/bencevans/screenshot-desktop/pull/284
- chore(deps): bump actions/checkout from 3 to 4 by @dependabot in https://github.com/bencevans/screenshot-desktop/pull/286
- chore(deps): bump actions/setup-node from 3 to 4 by @dependabot in https://github.com/bencevans/screenshot-desktop/pull/287
- chore(deps-dev): bump ava from 5.3.1 to 6.1.1 by @dependabot in https://github.com/bencevans/screenshot-desktop/pull/290
- chore(deps-dev): bump ava from 6.1.1 to 6.1.2 by @dependabot in https://github.com/bencevans/screenshot-desktop/pull/292
- chore(deps-dev): bump ava from 6.1.2 to 6.1.3 by @dependabot in https://github.com/bencevans/screenshot-desktop/pull/293
- chore(deps-dev): bump braces from 3.0.2 to 3.0.3 by @dependabot in https://github.com/bencevans/screenshot-desktop/pull/295
- chore(deps-dev): bump standard from 17.1.0 to 17.1.2 by @dependabot in https://github.com/bencevans/screenshot-desktop/pull/299
- chore(deps-dev): bump ava from 6.1.3 to 6.2.0 by @dependabot in https://github.com/bencevans/screenshot-desktop/pull/301
- fix(ci/linux): add imagemagick by @bencevans in https://github.com/bencevans/screenshot-desktop/pull/304
- fix: Long delay in listDisplays() xrandr call #302 by @mrtumnus in https://github.com/bencevans/screenshot-desktop/pull/303
New Contributors
- @mrtumnus made their first contribution in https://github.com/bencevans/screenshot-desktop/pull/303
Full Changelog: https://github.com/bencevans/screenshot-desktop/compare/v1.14.1...v1.15.1
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
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!
- Linux: required ImageMagick
- 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
filenameOptional. Absolute or relative path to save output.formatOptional. See supported image formats below. Values are case-insensitive.linuxLibraryOptional. Linux only. Valid valuesscrot|imagemagick. Which library to use. Note that scrot does not support screen selection.includeCursorOptional. macOS only. Include the mouse cursor in the capture. Windows and Linux reject it when enabled;falseis 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.
format | Windows | macOS | Linux: ImageMagick (default) | Linux: scrot |
|---|---|---|---|---|
jpg, jpeg | Yes | Yes | Yes | Yes |
png | Yes | Yes | Yes | Yes |
bmp | Yes | Yes | Yes | Yes |
tiff | No | Yes | Yes | Yes |
gif | No | Yes | Yes | Depends (see below) |
pdf | No | Yes | No | No |
“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