Sharp Library for Working with Images in Node.js
In modern web development, server-side image processing is a critical task. Node.js, thanks to its event-driven architecture, handles this task excellently. Among the many libraries for working with images, a special place is occupied by Sharp — a high-performance open-source module written in C++ and using the libvips library.
What is Sharp?
Sharp is a Node.js module for fast image processing. It supports JPEG, PNG, WebP, AVIF, TIFF, and GIF formats. Key features include resizing, cropping, rotation, color correction, watermarking, and format conversion. Thanks to the use of libvips, Sharp works 4-5 times faster than similar libraries (e.g., ImageMagick or GraphicsMagick).
Installation
Installation is done via npm. Make sure C++ compilers (gcc or clang) and libvips are installed on your system (some OS require preliminary installation).
npm install sharpAfter installation, you can import the module into your project:
const sharp = require('sharp');Basic Image Operations
1. Resize
The most common task is to reduce or enlarge an image. Sharp allows you to set width, height, or both dimensions simultaneously. If only one parameter is specified, the second is calculated proportionally.
const sharp = require('sharp');
async function resizeImage() { try { await sharp('input.jpg') .resize(300, 200) // width 300px, height 200px .toFile('output-resized.jpg'); console.log('Image resized'); } catch (error) { console.error('Error:', error); }}
resizeImage();To preserve proportions, use fit:
const sharp = require('sharp');
async function resizeWithFit() { await sharp('input.jpg') .resize({ width: 300, height: 200, fit: 'inside' // 'cover', 'contain', 'fill', 'inside', 'outside' }) .toFile('output-fit.jpg');}
resizeWithFit();2. Crop
Cropping allows you to remove unnecessary parts of an image. Sharp supports cropping by specified coordinates or by strategy (e.g., center crop).
const sharp = require('sharp');
async function cropImage() { await sharp('input.jpg') .extract({ left: 100, top: 50, width: 400, height: 300 }) .toFile('output-cropped.jpg');}
cropImage();You can also use resize with the crop option:
const sharp = require('sharp');
async function cropWithResize() { await sharp('input.jpg') .resize(200, 200, { fit: 'cover', position: 'centre' }) .toFile('output-cover.jpg');}
cropWithResize();3. Format Conversion
Sharp allows you to easily convert images from one format to another. Simply specify the extension in the output file or use the .toFormat() method.
const sharp = require('sharp');
async function convertToWebP() { await sharp('input.png') .toFormat('webp') .toFile('output.webp');}
async function convertToJpeg() { await sharp('input.webp') .jpeg({ quality: 80 }) .toFile('output.jpg');}
convertToWebP();convertToJpeg();4. Working with Metadata
You can extract image metadata: dimensions, color space, DPI, EXIF data, etc.
const sharp = require('sharp');
async function getMetadata() { const metadata = await sharp('input.jpg').metadata(); console.log('Metadata:', metadata); // { format: