API Reference

sharp

High-performance Node.js image processing using libvips for resize, format conversion and compositing.

Official Documentation ↗
CompatibilityThis documentation covers version sharp v0.33.x / Node 18 LTS(原生模块,依赖 libvips). Check the official docs for the latest API surface.

Installation

npmnpm install sharp
pnpmpnpm add sharp
yarnyarn add sharp

Methods

Overview

sharp processes images through libvips with a streaming, promise-based API. It resizes, converts and composites far faster and with lower memory than pure-JavaScript decoders, making it suitable for upload pipelines.

Core Methods

Create an instance from a path, buffer or stream, chain operations such as resize, rotate and format, then terminate with toFile, toBuffer or a stream. metadata inspects the image cheaply.

Usage Notes

Thumbnail on upload and store derived sizes rather than resizing on every request. Set fit and withoutEnlargement to avoid distortion, and add a pixel limit to defend against decompression bombs.

Common Pitfalls

Native install issues are common across platforms; CI images may need extra libraries. Ignoring EXIF orientation yields sideways photos. Re-encoding always costs CPU, so cache outputs instead of recomputing them.

FAQ

Why does sharp fail to install?
sharp ships prebuilt binaries for common platforms but falls back to compiling libvips when none match. Install build tools, or pin a version with a matching prebuild for your OS and architecture.
How do I handle EXIF orientation?
EXIF orientation is not applied automatically. Call .rotate() with no arguments to normalize the image according to its EXIF orientation tag before other transforms.
Is sharp safe for untrusted input?
It decodes images, which can be expensive or malicious. Limit input size, set a pixel limit to reject decompression bombs, and run processing with timeouts.