Sharp (Node.js)
lastmod 2026-11-18created 2026-11-184 edits
sharp is a high-performance image processing module for Node.js, developed by Lovell Fuller since 2013. It wraps the C image processing library libvips with Node-API and is mainly used to shrink large JPEG, PNG, and other images into web-sized JPEG, PNG, WebP, GIF, or AVIF files. Unlike approaches that call ImageMagick or GraphicsMagick as an external process, it uses libvips directly, and the project states that the same resize runs 4–5 times faster.
About
- Besides resizing, supports rotation, region extraction (extract), compositing multiple images (composite), gamma correction, and color space conversion. Preserves color spaces, embedded ICC profiles, and alpha channels, and keeps image quality with Lanczos resampling
- Accepts a file path, Buffer, or stream as input, and outputs with
toFile()ortoBuffer(). Methods are chained-
sharp('input.jpg').autoOrient().resize({ width: 200 }).jpeg({ mozjpeg: true }).toBuffer() - Used, for example, to add a watermark with
composite([{ input: watermark, gravity: 'southeast' }]), or to round corners by cutting along an SVG shape withblend: 'dest-in'
-
fit option of resize(): cover (default; keeps aspect ratio and crops the overflow), contain (keeps aspect ratio and pads the rest), fill (ignores aspect ratio and stretches), inside (keeps aspect ratio and fits within the box), outside (keeps aspect ratio and covers the box) metadata() reads the original width, height, and format, which can be used to shrink images only when the original is larger than a threshold node_modules folder built on a different OS must not be copied over as is. For Linux targets such as AWS Lambda, specify the target platform when installing, as in npm install --cpu=x64 --os=linux --libc=glibc sharp. On Lambda, CPU allocation grows with the configured memory, so a 1536MB function gets about 12 times as much CPU time as a 128MB one See also
- libvips
- Node.js
- Digital image processing
- wasm-vips - fast image processing library for browsers and NodeJS
Favorite site
- High performance Node.js image processing | sharp
- Github - lovell/sharp
- Installation | sharp
- (NODE) 📚 How to use the Sharp module - resizing images / adding watermarks
- Post from August 2022. Covers basic resizing, size limits with
metadata(), processing Buffers of remote images fetched with axios or node-fetch, resizing after a multer upload, and acompositewatermark example - Includes an example that builds a resize API of the form
/thumb/:size?fname=<이미지 URL>and a watermark API with Express. Because the server fetches whatever URL the user gives it, a real service needs an allowlist of domains - The
--platformand--archinstall options in the post are the pre-0.33 method; current versions use--os,--cpu, and--libc
- Post from August 2022. Covers basic resizing, size limits with